[leedcode 204] Count Primes
Description:
Count the number of prime numbers less than a non-negative number, n.
public class Solution { public int countPrimes(int n) { //筛选法,将坐标为1的倍数均置为0 if(n<=2)return 0; int temp[]=new int[n]; for(int i=2;i<n;i++){ temp[i]=1; } for(int i=2;i<n;i++){ if(temp[i]==0) continue; else{ for(int j=i+i;j<n;j=j+i) temp[j]=0; } } int res=0; for(int i=2;i<n;i++){ if(temp[i]!=0)res++; } return res; } }