GeezerVison is showing github.com — Add 204_Count_Primes Python and Java solution based on CPP Solution · iamsathish/leetcode@172fd36
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: README.md +1Lines changed: 1 addition & 0 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
@@ -81,6 +81,7 @@ Also, there are open source implementations for basic data structs and algorithm | 81 | 81 | | 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 | | 82 | 82 | | 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) | | 83 | 83 | | 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) | | 84 | 85 | | 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) | | 85 | 86 | | 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)| | 86 | 87 | | 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)| | |
Collapse file Copy file name to clipboardExpand all lines: cpp/204_Count_Primes.cpp +18-12Lines changed: 18 additions & 12 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | // Source : https://leetcode.com/problems/count-primes/ | 2 | 2 |
| 3 | | - | 4 | 3 | /********************************************************************************** | 5 | 4 | * | 6 | 5 | * Description: | | 113 | 112 | #include <vector> | 114 | 113 | using namespace std; | 115 | 114 |
| 116 | | -int countPrimes(int n) { | | 115 | +int countPrimes(int n) | | 116 | +{ | 117 | 117 | vector<bool> isPrimer(n, true); | 118 | 118 |
| 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 | + { | 122 | 125 | isPrimer[j] = false; | 123 | 126 | } | 124 | 127 | } | 125 | 128 | } | 126 | 129 |
| 127 | 130 | 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 | + { | 130 | 135 | //cout << i << ", "; | 131 | 136 | cnt++; | 132 | 137 | } | 133 | 138 | } | 134 | 139 | return cnt; | 135 | 140 | } | 136 | 141 |
| 137 | | - | 138 | | -int main(int argc, char**argv) | | 142 | +int main(int argc, char **argv) | 139 | 143 | { | 140 | 144 | int n = 100; | 141 | | - if (argc>1){ | | 145 | + if (argc > 1) | | 146 | + { | 142 | 147 | n = atoi(argv[1]); | 143 | 148 | } | 144 | | - | 145 | | - cout << endl << n << " : " << countPrimes(n) << endl; | | 149 | + | | 150 | + cout << endl | | 151 | + << n << " : " << countPrimes(n) << endl; | 146 | 152 |
| 147 | 153 | return 0; | 148 | 154 | } |
Collapse file Copy file name to clipboard +20Lines changed: 20 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 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 | +} |
Collapse file Copy file name to clipboardExpand all lines: python/204_Count_Primes.py +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -17,4 +17,4 @@ def countPrimes(self, n): | 17 | 17 | for i in xrange(2, n): | 18 | 18 | if isPrime[i]: | 19 | 19 | count += 1 | 20 | | - return count | | 20 | + return count |
|