204. Count Primes

不解释了

 1     public int countPrimes(int n) {
 2         if(n <= 0) {
 3             return 0;
 4         }
 5         boolean[] isPrime = new boolean[n];
 6         for(int i = 1; i < n; i++) {
 7             isPrime[i] = true;
 8         }
 9         for(int i = 2; i * i < n; i++) {
10             if(!isPrime[i]) {
11                 continue;
12             }
13             for(int j = i * i; j < n; j += i) {
14                 isPrime[j] = false;
15             }
16         }
17         int cnt = 0;
18         for(int i = 2; i < n; i++) {
19             if(isPrime[i]) {
20                 cnt++;
21             }
22         }
23         return cnt;
24     }

 

posted @ 2016-07-21 08:29  warmland  阅读(120)  评论(0编辑  收藏  举报