C# Solution, Similar as hint, 5 minutes coding


  • 0
    H

    public class Solution {

    public int CountPrimes(int n) {
        bool[] isPrime = new bool[n];
        
        //Initialize
        for( int i = 2; i < n; i++){
            isPrime[i] = true;
        }
        
        // mark-off
        for(int i = 2; i*i < n; i++){
            if( !isPrime[i]) continue;
            for( int j = i*i; j<n; j += i){
                isPrime[j] = false;
            }
        }
        
        // count
        int count = 0;
        for(int i = 2; i< n; i++){
            if( isPrime[i]) count++;
        }
        
        return count;
        
    }
    

    }


Log in to reply
 

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