Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit bc37db2

Browse files
计数质数
1 parent cd74080 commit bc37db2

File tree

1 file changed

+4
-5
lines changed

1 file changed

+4
-5
lines changed

‎204.计数质数.java‎

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,21 +27,20 @@
2727
// @lc code=start
2828
class Solution {
2929
public int countPrimes(int n) {
30-
if (n <= 2) {
30+
if (n < 2) {
3131
return 0;
3232
}
3333
boolean[] p = new boolean[n];
3434
Arrays.fill(p, true);
35-
double sqrt = Math.sqrt(n);
36-
for (int i = 2; i < sqrt; i++) {
35+
for (int i = 2; i * i < n; i++) {
3736
if (p[i]) {
38-
for (int j = i * i; j < p.length; j+=i) {
37+
for (int j = i + i; j < n; j += i) {
3938
p[j] = false;
4039
}
4140
}
4241
}
4342
int count = 0;
44-
for (int i = 2; i < p.length; i++) {
43+
for (int i = 2; i < n; i++) {
4544
if (p[i]) {
4645
count++;
4746
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /