11 lines C++ easy to understand


  • 0
    L
    class Solution {
    public:
        int countPrimes(int n) {
            vector<int>res(n,0);
            int cnt = 0;
            for(int i = 2; i < n;i++){
               if(res[i] == 0){
                   cnt++;
                   for(int j = 2; j*i < n;++j){
                       res[i*j] = 1;
                   }
               }
            }
            return cnt;
        }
    };
    

Log in to reply
 

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