当前位置:网站首页>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)
边栏推荐
- Mathematical formula (test)
- 企业级自定义表单引擎解决方案(十二)--表单规则引擎2
- A. Berland Poker & 1000 [simple mathematical thinking]
- 基于人脸识别的课堂考勤系统 tkinter+openpyxl+face_recognition
- Image 24 bits de profondeur à 8 bits de profondeur
- PHP MySQL inserts data
- Five problems of database operation in commodity supermarket system
- Line by line explanation of yolox source code of anchor free series network (5) -- mosaic data enhancement and mathematical understanding
- 2022-2028 global scar care product industry research and trend analysis report
- Getting started with deops
猜你喜欢
STM32 realizes 74HC595 control
2022-2028 global lithium battery copper foil industry research and trend analysis report
Prototype inheritance..
Research on Swift
Sensor debugging process
Class exercises
The number of incremental paths in the grid graph [dfs reverse path + memory dfs]
win32:堆破坏的dump文件分析
The vscode code is automatically modified to a compliance code when it is formatted and saved
Records of long objects and long judgments in the stream of list
随机推荐
Theoretical description of linear equations and summary of methods for solving linear equations by eigen
A. Odd Selection【BruteForce】
English grammar_ Noun classification
Keepalived 设置不抢占资源
Design limitations of structure type (struct)
网格图中递增路径的数目[dfs逆向路径+记忆dfs]
Research Report on investment trends and development planning of China's thermal insulation material industry, 2022-2028
2022-2028 global scar care product industry research and trend analysis report
PHP MySQL create database
Talk about the design and implementation logic of payment process
Closure and closure function
AcWing 271. 杨老师的照相排列【多维DP】
Research Report on competitive strategy Outlook Analysis and investment strategic planning of China's smart home equipment industry, 2022-2028
[combinatorics] generating function (positive integer splitting | repeated ordered splitting | non repeated ordered splitting | proof of the number of repeated ordered splitting schemes)
聊聊支付流程的设计与实现逻辑
PHP MySQL reads data
Class exercises
[combinatorics] generating function (shift property)
Graduation summary
Solve the problem of inaccurate network traffic monitored by ZABBIX with SNMP