Skip to content

Commit 17ac012

Browse files
committed
add CountPrimes204
1 parent 35f4dc7 commit 17ac012

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

Diff for: README.md

+3-2
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,11 @@
3232

3333

3434

35-
# Total: 104
35+
# Total: 105
3636

3737
| Easy | Medium | Hard |
3838
|:----:|:------:|:----:|
39-
| 18 | 62 | 24 |
39+
| 19 | 62 | 24 |
4040

4141

4242
| Question | Solution | Difficulty |
@@ -111,6 +111,7 @@
111111
| [147. Insertion Sort List](https://leetcode.com/problems/insertion-sort-list/) | [Solution](https://github.com/fluency03/leetcode-java/blob/master/src/InsertionSortList147.java) | Medium |
112112
| [169. Majority Element](https://leetcode.com/problems/majority-element/) | [Solution](https://github.com/fluency03/leetcode-java/blob/master/src/MajorityElement169.java) | Easy |
113113
| [198. House Robber](https://leetcode.com/problems/house-robber/) | [Solution](https://github.com/fluency03/leetcode-java/blob/master/src/HouseRobber198.java) | Easy |
114+
| [204. Count Primes](https://leetcode.com/problems/count-primes/) | [Solution](https://github.com/fluency03/leetcode-java/blob/master/src/CountPrimes204.java) | Easy |
114115
| [208. Implement Trie (Prefix Tree)](https://leetcode.com/problems/implement-trie-prefix-tree/) | [Solution](https://github.com/fluency03/leetcode-java/blob/master/src/Trie.java) | Medium |
115116
| [212. Word Search II](https://leetcode.com/problems/word-search-ii/) | [Solution](https://github.com/fluency03/leetcode-java/blob/master/src/WordSearchII212.java) | Hard |
116117
| [215. Kth Largest Element in an Array](https://leetcode.com/problems/kth-largest-element-in-an-array/) | [Solution](https://github.com/fluency03/leetcode-java/blob/master/src/KthLargestElementInAnArray215.java) | Medium |

Diff for: src/CountPrimes204.java

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* Description:
3+
*
4+
* Count the number of prime numbers less than a non-negative number, n.
5+
*/
6+
7+
8+
public class CountPrimes204 {
9+
public int countPrimes(int n) {
10+
boolean[] notPrime = new boolean[n];
11+
int count = 0;
12+
for (int i = 2; i < n; i++) {
13+
if (notPrime[i] == false) {
14+
count++;
15+
for (int j = 2; i*j < n; j++) {
16+
notPrime[i*j] = true;
17+
}
18+
}
19+
}
20+
21+
return count;
22+
}
23+
}

0 commit comments

Comments
 (0)