Description:
Count the number of prime numbers less than a non-negative number, n.
Credits:
Special thanks to @mithmatt for adding this problem and creating all test cases.
不敢说是自己的思路。
我发现我刷easy时现在的问题时,不敢提空间复杂度。
记得用各种东西来存数据啊啊啊啊!!问题会简单很多啊啊啊!!
public class Solution {
public int countPrimes(int n) {
int[] isPrime = new int[n];
if (n<=2)
return 0;
for(int i = 0; i < n; i++)
isPrime[i] = 1;
for(int i = 2; i < n; i++){
int num = i*2;
while(num < n){
isPrime[num] = 0;
num += i;
}
}
int count = 0;
for(int i = 2; i < n; i++){
if (isPrime[i] == 1)
count++;
}
return count;
}
}