2 Star 10 Fork 2

CG国斌 / myleetcode

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
_204.java 868 Bytes
一键复制 编辑 原始数据 按行查看 历史
CG国斌 提交于 2020-10-18 09:51 . 维护简单问题结婚
package com.hit.basmath.interview.top_interview_questions.easy_collection.math;
/**
* 204. 计数质数
* <p>
* 统计所有小于非负整数 n 的质数的数量。
* <p>
* 示例 1:
* <p>
* 输入:n = 10
* 输出:4
* 解释:小于 10 的质数一共有 4 个, 它们是 2, 3, 5, 7 。
* <p>
* 示例 2:
* <p>
* 输入:n = 0
* 输出:0
* <p>
* 示例 3:
* <p>
* 输入:n = 1
* 输出:0
* <p>
* 提示:
* <p>
* 0 <= n <= 5 * 10^6
*/
public class _204 {
public int countPrimes(int n) {
boolean[] notPrime = new boolean[n];
int count = 0;
for (int i = 2; i < n; i++) {
if (!notPrime[i]) {
count++;
for (int j = 2; i * j < n; j++) {
notPrime[i * j] = true;
}
}
}
return count;
}
}
Java
1
https://gitee.com/guobinhit/myleetcode.git
git@gitee.com:guobinhit/myleetcode.git
guobinhit
myleetcode
myleetcode
master

搜索帮助