当前位置:网站首页>204. Count prime
204. Count prime
2022-07-03 18:19:00 【The_ Dan】
Method 1 Linear sieve
class Solution {
public:
int countPrimes(int n) {
vector<int> vecInt;
vector<bool> flag(n, true);
int i, j;
for(i = 2; i < n; i++){
if(flag[i])
vecInt.push_back(i);
for(j = 0; j < vecInt.size() && i * vecInt[j] < n; j++){
flag[i * vecInt[j]] = false;
if(i % vecInt[j] == 0){
break;
}
}
}
return vecInt.size();
}
};
Accepted
66/66 cases passed (544 ms)
Your runtime beats 17.94 % of cpp submissions
Your memory usage beats 52.16 % of cpp submissions (36.3 MB)
Method 2 Ethmoid
class Solution {
public:
int countPrimes(int n) {
vector<bool> flag(n, true);
int ans = 0;
for(auto i = 2; i < n; i++){
if(flag[i]){
ans++;
if((long long)i * i < n){
for(auto j = i * i; j < n; j += i){
flag[j] = false;
}
}
}
}
return ans;
}
};
Accepted
66/66 cases passed (156 ms)
Your runtime beats 91.09 % of cpp submissions
Your memory usage beats 93.42 % of cpp submissions (10 MB)
边栏推荐
- 一入“远程”终不悔,几人欢喜几人愁。| 社区征文
- OpenSSL的SSL/BIO_get_fd
- Computer graduation design PHP sports goods online sales system website
- [Yu Yue education] family education SPOC class 2 reference materials of Shanghai Normal University
- Valentine's day, send you a little red flower~
- ES7 - Optimization of promise
- WebView module manages the application window interface to realize the logical control and management operation of multiple windows (Part 1)
- 2022-2028 global copper foil (thickness 12 μ M) industry research and trend analysis report
- Research Report on investment trends and development planning of China's thermal insulation material industry, 2022-2028
- As soon as we enter "remote", we will never regret, and several people will be happy and several people will be sad| Community essay solicitation
猜你喜欢
Win32: analyse du fichier dump pour la défaillance du tas
win32:堆破壞的dump文件分析
2022-2028 global petroleum pipe joint industry research and trend analysis report
Computer graduation design PHP campus address book telephone number inquiry system
How to expand the capacity of golang slice slice
MySQL has been stopped in the configuration interface during installation
STM32 realizes 74HC595 control
Global and Chinese pediatric palliative care drug market development research and investment planning recommendations report 2022-2028
[combinatorics] generating function (generating function application scenario | using generating function to solve recursive equation)
Lesson 13 of the Blue Bridge Cup -- tree array and line segment tree [exercise]
随机推荐
[combinatorics] generating function (example of generating function | calculating generating function with given general term formula | calculating general term formula with given generating function)
[combinatorics] generating function (use generating function to solve the combination number of multiple sets R)
[combinatorics] generating function (positive integer splitting | repeated ordered splitting | non repeated ordered splitting | proof of the number of repeated ordered splitting schemes)
Keepalived 设置不抢占资源
How to install PHP on Ubuntu 20.04
The second largest gay dating website in the world was exposed, and the status of programmers in 2022
Summary and Reflection on the third week of winter vacation
Research on Swift
2022-2028 global lithium battery copper foil industry research and trend analysis report
Market demand survey and marketing strategy analysis report of global and Chinese pet milk substitutes 2022-2028
Mature port AI ceaspectus leads the world in the application of AI in terminals, CIMC Feitong advanced products go global, smart terminals, intelligent ports, intelligent terminals
On Data Mining
MySQL has been stopped in the configuration interface during installation
Discussion sur la logique de conception et de mise en oeuvre du processus de paiement
Redis core technology and practice - learning notes (IX): slicing cluster
How to deploy applications on kubernetes cluster
Prototype inheritance..
Grammaire anglaise Nom - Classification
English grammar_ Adjective / adverb Level 3 - multiple expression
The number of incremental paths in the grid graph [dfs reverse path + memory dfs]