当前位置:网站首页>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)
边栏推荐
- Postfix tips and troubleshooting commands
- Class exercises
- Sensor 调试流程
- Global and Chinese health care OEM and ODM market status survey and investment planning recommendations report 2022-2028
- PHP MySQL reads data
- Computer graduation design PHP campus address book telephone number inquiry system
- Ml (machine learning) softmax function to realize the classification of simple movie categories
- [combinatorics] generating function (positive integer splitting | unordered | ordered | allowed repetition | not allowed repetition | unordered not repeated splitting | unordered repeated splitting)
- Kotlin's collaboration: Context
- How to track the real-time trend of Bank of London
猜你喜欢

Research Report on investment trends and development planning of China's thermal insulation material industry, 2022-2028

Research Report on market demand and investment planning for the development of China's office chair industry, 2022-2028

2022-2028 global lithium battery copper foil industry research and trend analysis report

The vscode code is automatically modified to a compliance code when it is formatted and saved

2022-2028 global copper foil (thickness 12 μ M) industry research and trend analysis report

2022-2028 global physiotherapy clinic industry research and trend analysis report

SQL injection -day16
![Bloom filter [proposed by bloom in 1970; redis cache penetration solution]](/img/f9/27a75454b464d59b9b3465d25fe070.jpg)
Bloom filter [proposed by bloom in 1970; redis cache penetration solution]

How to analyze the rising and falling rules of London gold trend chart

Research Report on competitive strategy Outlook Analysis and investment strategic planning of China's smart home equipment industry, 2022-2028
随机推荐
Investigation on the operation prospect of the global and Chinese Anti enkephalinase market and analysis report on the investment strategy of the 14th five year plan 2022-2028
Talk about the design and implementation logic of payment process
Research Report on investment trends and development planning of China's thermal insulation material industry, 2022-2028
企业级自定义表单引擎解决方案(十二)--表单规则引擎2
Ssl/bio of OpenSSL_ get_ fd
PHP MySQL preprocessing statement
OpenSSL的SSL/BIO_get_fd
[Yu Yue education] family education SPOC class 2 reference materials of Shanghai Normal University
Research Report on market demand and investment planning for the development of China's office chair industry, 2022-2028
图像24位深度转8位深度
Sensor debugging process
Codeforces Round #803 (Div. 2) C. 3SUM Closure
A. Berland Poker &1000【简单数学思维】
Graduation summary
Redis core technology and practice - learning notes (VI) how to achieve data consistency between master and slave Libraries
English语法_形容词/副词3级 - 倍数表达
2022-2028 global aircraft head up display (HUD) industry research and trend analysis report
SDNUOJ1015
Redis core technology and practice - learning notes (11): why not just string
解决Zabbix用snmp监控网络流量不准的问题