File tree 2 files changed +26
-0
lines changed
2 files changed +26
-0
lines changed Original file line number Diff line number Diff line change 90
90
191|[ Number of 1 Bits] ( ./0191-number-of-1-bits.js ) |Easy|
91
91
198|[ House Robber] ( ./0198-house-robber.js ) |Medium|
92
92
203|[ Remove Linked List Elements] ( ./0203-remove-linked-list-elements.js ) |Easy|
93
+ 204|[ Count Primes] ( ./0204-count-primes.js ) |Medium|
93
94
206|[ Reverse Linked List] ( ./0206-reverse-linked-list.js ) |Easy|
94
95
213|[ House Robber II] ( ./0213-house-robber-ii.js ) |Medium|
95
96
214|[ Shortest Palindrome] ( ./0214-shortest-palindrome.js ) |Hard|
Original file line number Diff line number Diff line change
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
+ } ;
You can’t perform that action at this time.
0 commit comments