leetcode学习笔记20

204. Count Primes

Count the number of prime numbers less than a non-negative number, n.
借鉴大佬的代码

class Solution {
    public int countPrimes(int n) {
        boolean[] notPrime=new boolean[n];
        int count=0;
        for(int i=2;i<n;i++){
        	   if(notPrime[i]==false) {
        		   count++;
                   for(int j=i;j<n;j+=i) {
                       notPrime[j]=true;
                   }
               }
        		   
        }
        return count;
    }
}

猜你喜欢

转载自blog.csdn.net/weixin_38941866/article/details/84999930
今日推荐