My Java solution


  • 0
    A

    beats 46%
    ···
    public class Solution {
    public int countPrimes(int n) {
    if (n < 3) {
    return 0;
    }
    if (n < 4) {
    return 1;
    }
    int count = 1;
    boolean unis[] = new boolean[n];
    int list[] = new int[n];
    int index = 0;
    for (int i = 3; i < n; i = i + 2) {
    if (unis[i] == false) {
    list[index++] = i;
    count++;
    }
    for (int j = 0; j < index && n > list[j] * i; j++) {
    unis[list[j] * i] = true;
    }
    }
    return count;
    }
    }
    ···


Log in to reply
 

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