Description:
Count the number of PRime numbers less than a non-negative number, n.
public class Solution { public int countPrimes(int n) { boolean[] isPrimes = new boolean[n]; int res = 0; for (int i = 2; i < n; i++) isPrimes[i] = true; for (int i = 2; i * i < n; i++) { for (int j = i * i; j < n; j += i) { if (!isPrimes[j]) continue; isPrimes[j] = false; } } for (int i = 2; i < n; i++) { if (isPrimes[i]) res++; } return res; }}新闻热点
疑难解答