Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 172fd36

Browse files
committed
Add 204_Count_Primes Python and Java solution based on CPP Solution
1 parent 0081e4b commit 172fd36

File tree

4 files changed

+40
-13
lines changed

4 files changed

+40
-13
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ Also, there are open source implementations for basic data structs and algorithm
8181
| 186 | [Reverse Words in a String II](https://leetcode.com/problems/reverse-words-in-a-string-ii/) ♥| [Python](https://github.com/qiyuangong/leetcode/blob/master/python/186_Reverse_Words_in_a_String_II.py) | Reverse all and reverse each words |
8282
| 198 | [House Robber](https://leetcode.com/problems/house-robber/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/198_House_Robber.py) | f(k) = max(f(k – 2) + num[k], f(k – 1)), O(n) and O(1) |
8383
| 200 | [Number of Islands](https://leetcode.com/problems/number-of-islands/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/200_Number_of_Islands.py) | 1. Quick union find, O(nlogn) and O(n^2)<br>2. BFS with marks, O(n^2) and O(1) |
84+
| 204 | [Count Primes](https://leetcode.com/problems/count-primes/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/204_Count_Primes.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/204_Count_Primes.java) [CPP](https://github.com/qiyuangong/leetcode/blob/master/cpp/204_Count_Primes.cpp) | [Sieve of Eratosthenes](https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes#Algorithm_complexity), O(nloglogn) and O(n) |
8485
| 206 | [Reverse Linked List](https://leetcode.com/problems/reverse-linked-list/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/206_Reverse_Linked_List.py) | 1. Stack, O(n) and O(n)<br>2. Traverse on prev and curr, then curr.next = prev, O(n) and O(1)<br>3. Recursion, O(n) and O(1) |
8586
| 213 | [House Robber II](https://leetcode.com/problems/house-robber-ii/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/213_House_Robber_II.py) | f(k) = max(f(k – 2) + num[k], max(dp[0~ls-2],dp[1~ls-1], O(n) and O(1)|
8687
| 215 | [Kth Largest Element in an Array](https://leetcode.com/problems/kth-largest-element-in-an-array/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/215_Kth_Largest_Element_in_an_Array.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/215_Kth_Largest_Element_in_an_Array.java) | 1. Sort, O(n) and O(n)<br>2. Heap, O(nlgk) and O(n)<br>3. Quick selection, O(klgn) and O(n)|

CountingPrimes.cpp renamed to cpp/204_Count_Primes.cpp

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
// Source : https://leetcode.com/problems/count-primes/
22

3-
43
/**********************************************************************************
54
*
65
* Description:
@@ -113,36 +112,43 @@
113112
#include <vector>
114113
using namespace std;
115114

116-
int countPrimes(int n) {
115+
int countPrimes(int n)
116+
{
117117
vector<bool> isPrimer(n, true);
118118

119-
for(int i=2; i*i<n; i++){
120-
if (isPrimer[i]){
121-
for(int j=i*i; j<n; j+=i){
119+
for (int i = 2; i * i < n; i++)
120+
{
121+
if (isPrimer[i])
122+
{
123+
for (int j = i * i; j < n; j += i)
124+
{
122125
isPrimer[j] = false;
123126
}
124127
}
125128
}
126129

127130
int cnt = 0;
128-
for(int i=2; i<n; i++){
129-
if (isPrimer[i]) {
131+
for (int i = 2; i < n; i++)
132+
{
133+
if (isPrimer[i])
134+
{
130135
//cout << i << ", ";
131136
cnt++;
132137
}
133138
}
134139
return cnt;
135140
}
136141

137-
138-
int main(int argc, char**argv)
142+
int main(int argc, char **argv)
139143
{
140144
int n = 100;
141-
if (argc>1){
145+
if (argc > 1)
146+
{
142147
n = atoi(argv[1]);
143148
}
144-
145-
cout << endl << n << " : " << countPrimes(n) << endl;
149+
150+
cout << endl
151+
<< n << " : " << countPrimes(n) << endl;
146152

147153
return 0;
148154
}

java/204_Count_Primes.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution {
2+
// ttps://en.wikipedia.org/wiki/Sieve_of_Eratosthenes#Algorithm_complexity
3+
public int countPrimes(int n) {
4+
boolean[] isPrime = new boolean[n];
5+
int count = 0;
6+
Arrays.fill(isPrime, true);
7+
for (int i = 2; i < n; i++) {
8+
if (i * i >= n)
9+
break;
10+
if (!isPrime[i])
11+
continue;
12+
for (int j = i * i; j < n; j += i)
13+
isPrime[j] = false;
14+
}
15+
for (int i = 2; i < n; i++)
16+
if (isPrime[i])
17+
count++;
18+
return count;
19+
}
20+
}

python/204_Count Primes.py renamed to python/204_Count_Primes.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,4 @@ def countPrimes(self, n):
1717
for i in xrange(2, n):
1818
if isPrime[i]:
1919
count += 1
20-
return count
20+
return count

0 commit comments

Comments
 (0)