Be aware that n is not included


  • 1
    W
     public class Solution {
            public int countPrimes(int n) {
                if(n<2){
        			return 0;
        		}
                BitSet AllNumbersSet=new BitSet(n);  
                AllNumbersSet.clear();
                for(int i=2;i<=n/2;i++){
                	for(int j=i*2;j<n;j=j+i){
                		AllNumbersSet.set(j);
                	}
                }
                return n-AllNumbersSet.cardinality()-2; 
            }
        }

Log in to reply
 

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