Count primes


  • 0
    V

    class Solution {
    public int countPrimes(int n) {
    //n = n-1;
    if(n<2) return 0;
    boolean[] primes = new boolean[n];
    Arrays.fill(primes,true);
    primes[0]=primes[1]=false;
    for(int i=2; i<primes.length;i++){
    if(primes[i])
    {
    for(int j = 2; ij < primes.length; j++){
    primes[i
    j] = false;
    }
    }
    }
    int count = 0;
    for(int i=0 ;i<primes.length;i++)
    {
    if(primes[i]){
    count++;
    }
    }
    return count;
    }
    }


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.