Why my answer is Wrong


  • 0
    W
    public class Solution {
        public int countPrimes(int n) {
            if(n<=1)return 0;
            int sum=0;
            boolean mark[]=new boolean[n];
            Arrays.fill(mark, true);
            for(int i=2;i<n;++i) {
                if(mark[i]) {
                    sum++;
                    int x=i;
                    while(x*i<n && x*i>=0) {
                        mark[x*i]=false;
                        x++;
                    }
                } else {
                    
                }
            }
            return sum;
        }
    }
    

    The above code gives wrong answer, but if I use

    int x = 1
    

    It is AC. I want to know why?


Log in to reply
 

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