Skip to content

Commit 5da3422

Browse files
committedJan 24, 2022
Add solution #204
1 parent 18e53c6 commit 5da3422

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed
 

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@
9090
191|[Number of 1 Bits](./0191-number-of-1-bits.js)|Easy|
9191
198|[House Robber](./0198-house-robber.js)|Medium|
9292
203|[Remove Linked List Elements](./0203-remove-linked-list-elements.js)|Easy|
93+
204|[Count Primes](./0204-count-primes.js)|Medium|
9394
206|[Reverse Linked List](./0206-reverse-linked-list.js)|Easy|
9495
213|[House Robber II](./0213-house-robber-ii.js)|Medium|
9596
214|[Shortest Palindrome](./0214-shortest-palindrome.js)|Hard|

‎solutions/0204-count-primes.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* 204. Count Primes
3+
* https://leetcode.com/problems/count-primes/
4+
* Difficulty: Medium
5+
*
6+
* Given an integer n, return the number of prime numbers that are strictly less than n.
7+
*/
8+
9+
/**
10+
* @param {number} n
11+
* @return {number}
12+
*/
13+
var countPrimes = function(n) {
14+
const nonPrimes = [];
15+
let result = 0;
16+
for (let i = 2; i < n; i++) {
17+
if (!nonPrimes[i]) {
18+
result++;
19+
for (let j = 2; i * j < n; j++) {
20+
nonPrimes[i * j] = 1;
21+
}
22+
}
23+
}
24+
return result;
25+
};

0 commit comments

Comments
 (0)
Please sign in to comment.