Skip to content

Commit d989e12

Browse files
add js solution for 204
1 parent 08b2ca4 commit d989e12

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

solution/204.Count Primes/Solution.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// 600ms多
2+
const countPrimes2 = function(n){
3+
let arr = [];
4+
let res = 0;
5+
for(let i = 2; i < n; i++){
6+
if(arr[i] === undefined){
7+
arr[i] = 1;
8+
res++;
9+
for(let j = 2; i * j < n; j++){
10+
arr[i * j] = 0;
11+
}
12+
}
13+
}
14+
return res;
15+
};
16+
17+
//200ms多
18+
const countPrimes = function(n){
19+
let arr = [];
20+
let res = 0;
21+
for(let i = 2; i < n; i++){
22+
if(arr[i] === undefined){
23+
arr[i] = 1;
24+
res++;
25+
for(let j = i; i * j < n; j++){
26+
arr[i * j] = 0;
27+
}
28+
}
29+
}
30+
return res;
31+
};

0 commit comments

Comments
 (0)