JAVA-------Easy Version To Understand!!!


  • 1
    H
    	public static int countPrimes(int n) {
    	if (n <= 1)
    		return 0;
    	int[] flag = new int[n];
    	for (int i = 2; i < n; i++)
    		flag[i] = 1;
    	for (int i = 2; i <= (int) Math.sqrt(n); i++) {
    		if (flag[i] == 1) {
    			for (int j = i; j * i < n; j++) {
    				flag[j * i] = 0;
    			}
    		}
    	}
    	int count = 0;
    	for (int i = 2; i < n; i++) {
    		if (flag[i] == 1)
    			count++;
    	}
    	return count;
    }

  • 0
    J

    TAT I used this solution with Python and got TLE. Poor python!


  • 0
    H

    Sometimes,different language can result in different result.


  • 0
    J

    Yes, I got an AC with c++. Thank you!


Log in to reply
 

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