JAVA SOLUTION


  • 0
    3
            public int countPrimes(int n) {
    		int count = 0;
    		boolean[] f = new boolean[n];
    		for (int i = 2; i < n; i++) {
    			if(!f[i]){
    				for (int j = 2; j*i < n; j++) {
    					f[j*i] = true;
    				}
    				count++;
    			}
    		}
    		return count;
    	}
    

  • 0
    S
     public int countPrimes(int n) {
           
            boolean[] notPrimes = new boolean[n];
            notPrimes[0] = true;
            notPrimes[1] = true;
            int count = 0 ;
            for(int i = 2 ; i < n && !notPrimes[i] ; i++){
                count++;
                for(int j = 2 ; i * j < n ; j++){
                    notPrimes[i*j] = true;
                } 
            }
            return count;
        }```

  • 0
    3

    @saneGuy why n<3 return 0, 2 not is primes?


  • 0
    S

    @363655707 changed it ...


Log in to reply
 

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