Count Primes Total Accepted: 831 Total Submissions: 6167
来源:互联网 发布:淘宝促销在哪里设置 编辑:程序博客网 时间:2024/06/05 07:44
题目来自:Leetcode
https://leetcode.com/problems/count-primes/
Count Primes
Total Accepted: 831 Total Submissions: 6167My SubmissionsDescription:
Count the number of prime numbers less than a non-negative number, n
Hint: The number n could be in the order of 100,000 to 5,000,000.
click to show more hints.
References:
How Many Primes Are There?
Sieve of Eratosthenes
Credits:
Special thanks to @mithmatt for adding this problem and creating all test cases.
Have you met this question in a real interview?
Yes
No
Discuss
class Solution {public: int countPrimes(int n) { if(n<=2) return 0; if(n==3) return 1; bitset<5000001> b; b.set(); b.reset(0); b.reset(1); int imax=sqrt(n)+1; int j; for(int i=2;i<=imax;++i) { if(b.test(i)==true) { j=i*i; while(j<n) { b.reset(j); // cout<<"j="<<j<<endl; j+=i; } } } b.flip(); return n-b.count(); }};
0 0
- Count Primes Total Accepted: 831 Total Submissions: 6167
- leetcode: Total Accepted: 4338 Total Submissions: 20460
- Path Sum Total Accepted: 50216 Total Submissions: 168425
- Minimum Path Sum Total Accepted: 34975 Total Submissions: 109325
- H-Index Total Accepted: 3191 Total Submissions: 14098
- Remove Duplicates from Sorted List Total Accepted: 40004 Total Submissions: 116304
- Bitwise AND of Numbers Range Total Accepted: 2859 Total Submissions: 11145
- Populating Next Right Pointers in Each Node Total Accepted: 46429 Total Submissions: 128383
- Flatten Binary Tree to Linked List Total Accepted: 45093 Total Submissions: 156588
- poj 3617 贪心 不难 容易错 Total Submissions: 8060Accepted: 2489
- leetcode Intersection of Two Linked Lists My Submissions Question Solution Total Accepted: 43555 To
- Language: Cable master Time Limit: 1000MSMemory Limit: 10000K Total Submissions: 46653Accepted:
- Excuses, Excuses! Time Limit: 1000MSMemory Limit: 10000K Total Submissions: 4346Accepted: 1497 D
- total
- poj3641Pseudoprime numbers Time Limit: 1000MSMemory Limit: 65536K Total Submissions: 8854Accepte
- qmail total
- Total Commander
- Total Commander
- 进程与线程的一个简单解释
- HDU 1062 — Text Reverse
- 安卓学习第十三天:大文件的IO,字符流,处理流,内部类,匿名内部类,线程初步
- 第6题
- 校赛总结
- Count Primes Total Accepted: 831 Total Submissions: 6167
- typedef关键字
- Spring安全权限管理(Spring Security)
- 【连载】Java学习系列(004)——程序逻辑-2(分支和循环)
- C语言笔记之头文件与链接(二)
- 计算机是如何启动的?
- APP_FIELD.CLEAR_DEPENDENT_FIELDS
- template
- 博客中的理解程度