My Ac c++ code,95ms


  • 0
    L
    class Solution {
    public:
        int countPrimes(int n) {
            if(n<3)
                return 0;
            int *flag=new int[n];
            fill(flag,flag+n,1);
            int c=n-2,m=n/2;
            for(int i=2;i<=m;i++)
            {
                if(flag[i])
                {
                    for(int j=2;i*j<n;j++)
                    {
                        if(flag[i*j])
                        {
                            flag[i*j]=0;
                            c--;
                        }
                    }
                }
            }
            delete []flag;
            return c;
        }
    };

Log in to reply
 

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