204:计数质数
This commit is contained in:
parent
d1f5222acc
commit
444a999efe
76
src/main/java/leetcode/editor/cn/CountPrimes.java
Normal file
76
src/main/java/leetcode/editor/cn/CountPrimes.java
Normal file
@ -0,0 +1,76 @@
|
|||||||
|
//给定整数 n ,返回 所有小于非负整数 n 的质数的数量 。
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 示例 1:
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//输入:n = 10
|
||||||
|
//输出:4
|
||||||
|
//解释:小于 10 的质数一共有 4 个, 它们是 2, 3, 5, 7 。
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 示例 2:
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//输入:n = 0
|
||||||
|
//输出:0
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 示例 3:
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//输入:n = 1
|
||||||
|
//输出:0
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 提示:
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 0 <= n <= 5 * 10⁶
|
||||||
|
//
|
||||||
|
// Related Topics 数组 数学 枚举 数论 👍 869 👎 0
|
||||||
|
|
||||||
|
package leetcode.editor.cn;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
|
||||||
|
//204:计数质数
|
||||||
|
public class CountPrimes {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
Solution solution = new CountPrimes().new Solution();
|
||||||
|
// TO TEST
|
||||||
|
solution.countPrimes(499979);
|
||||||
|
}
|
||||||
|
|
||||||
|
//leetcode submit region begin(Prohibit modification and deletion)
|
||||||
|
class Solution {
|
||||||
|
public int countPrimes(int n) {
|
||||||
|
if (n <= 2) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
boolean[] nums = new boolean[n + 1];
|
||||||
|
Arrays.fill(nums, true);
|
||||||
|
nums[0] = false;
|
||||||
|
nums[1] = false;
|
||||||
|
int count = 0;
|
||||||
|
int max = (int) Math.sqrt(n);
|
||||||
|
for (int i = 2; i < n; i++) {
|
||||||
|
if (nums[i]) {
|
||||||
|
count++;
|
||||||
|
if (i > max) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
for (int j = i; j * i < n; j++) {
|
||||||
|
nums[j * i] = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return count;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//leetcode submit region end(Prohibit modification and deletion)
|
||||||
|
|
||||||
|
}
|
34
src/main/java/leetcode/editor/cn/doc/content/CountPrimes.md
Normal file
34
src/main/java/leetcode/editor/cn/doc/content/CountPrimes.md
Normal file
@ -0,0 +1,34 @@
|
|||||||
|
<p>给定整数 <code>n</code> ,返回 <em>所有小于非负整数 <code>n</code> 的质数的数量</em> 。</p>
|
||||||
|
|
||||||
|
<p> </p>
|
||||||
|
|
||||||
|
<p><strong>示例 1:</strong></p>
|
||||||
|
|
||||||
|
<pre>
|
||||||
|
<strong>输入:</strong>n = 10
|
||||||
|
<strong>输出:</strong>4
|
||||||
|
<strong>解释:</strong>小于 10 的质数一共有 4 个, 它们是 2, 3, 5, 7 。
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
<p><strong>示例 2:</strong></p>
|
||||||
|
|
||||||
|
<pre>
|
||||||
|
<strong>输入:</strong>n = 0
|
||||||
|
<strong>输出:</strong>0
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
<p><strong>示例 3:</strong></p>
|
||||||
|
|
||||||
|
<pre>
|
||||||
|
<strong>输入:</strong>n = 1
|
||||||
|
<strong>输出</strong>:0
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
<p> </p>
|
||||||
|
|
||||||
|
<p><strong>提示:</strong></p>
|
||||||
|
|
||||||
|
<ul>
|
||||||
|
<li><code>0 <= n <= 5 * 10<sup>6</sup></code></li>
|
||||||
|
</ul>
|
||||||
|
<div><div>Related Topics</div><div><li>数组</li><li>数学</li><li>枚举</li><li>数论</li></div></div><br><div><li>👍 869</li><li>👎 0</li></div>
|
Loading…
Reference in New Issue
Block a user