首页 > 学院 > 开发设计 > 正文

204. Count Primes

2019-11-08 19:46:59
字体:
来源:转载
供稿:网友

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; }}
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表