LeetCode - 204. Count Primes
2016-12-31 17:21
435 查看
Description:
Count the number of prime numbers less than a non-negative number, n.
Credits:
Special thanks to @mithmatt for adding this problem and creating all test cases.
统计小于n的素数个数,直接枚举会超时。这里使用素数筛
class Solution {
public:
int countPrimes(int n) {
int *table = new int
;
table[2] = 0;
int count = 0;
for( int i = 2; i < n; i++ ) {
if( table[i] == 0 ) {
count++;
for( int j = i * 2; j < n; j += i ) {
table[j] = 1;
}
}
}
return count;
}
};
Count the number of prime numbers less than a non-negative number, n.
Credits:
Special thanks to @mithmatt for adding this problem and creating all test cases.
统计小于n的素数个数,直接枚举会超时。这里使用素数筛
class Solution {
public:
int countPrimes(int n) {
int *table = new int
;
table[2] = 0;
int count = 0;
for( int i = 2; i < n; i++ ) {
if( table[i] == 0 ) {
count++;
for( int j = i * 2; j < n; j += i ) {
table[j] = 1;
}
}
}
return count;
}
};
相关文章推荐
- LeetCode 204. Count Primes
- Leetcode 204. Count Primes
- [LeetCode]204. Count Primes
- <LeetCode OJ> 204. Count Primes
- 【LeetCode】204. Count Primes
- LeetCode 204. Count Primes
- 【LeetCode】 204. Count Primes
- Leetcode 204. Count Primes
- LeetCode 204. Count Primes
- LeetCode 204. Count Primes
- leetcode204. Count Primes
- leetcode 204. Count Primes 这道题很有趣 + 搜索空间递减
- leetcode 204. Count Primes
- leetcode 204. Count Primes
- [leetcode] 204. Count Primes
- LeetCode204. Count Primes
- leetcode 204. Count Primes
- Leetcode-204. Count Primes
- [leetcode]: 204. Count Primes
- LeetCode 204. Count Primes