当前位置:网站首页>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)
边栏推荐
- Research Report on market demand and investment planning for the development of China's office chair industry, 2022-2028
- Class exercises
- 2022-2028 global aircraft head up display (HUD) industry research and trend analysis report
- 企业级自定义表单引擎解决方案(十二)--表单规则引擎2
- Five problems of database operation in commodity supermarket system
- Computer graduation design PHP campus address book telephone number inquiry system
- supervisor监控Gearman任务
- A. Odd Selection【BruteForce】
- How to install PHP on Ubuntu 20.04
- English語法_名詞 - 分類
猜你喜欢

2022-2028 global scar care product industry research and trend analysis report

win32:堆破壞的dump文件分析

How to expand the capacity of golang slice slice

Redis core technology and practice - learning notes (VIII) sentinel cluster: sentinel hung up

MySQL has been stopped in the configuration interface during installation

Talk about the design and implementation logic of payment process

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

聊聊支付流程的設計與實現邏輯

Redis core technology and practice - learning notes (IX): slicing cluster

Redis on local access server
随机推荐
What London Silver Trading software supports multiple languages
Remote office tools sharing | community essay solicitation
[linux]centos 7 reports an error when installing MySQL "no package MySQL server available" no package ZABBIX server MySQL available
Design limitations of structure type (struct)
Class exercises
Gear2021 monthly update - December
Applet with multiple tabs and Swipers + paging of each tab
Kotlin's collaboration: Context
PHP MySQL Update
Ssl/bio of OpenSSL_ get_ fd
小程序 多tab 多swiper + 每个tab分页
[combinatorics] generating function (example of generating function | calculating generating function with given general term formula | calculating general term formula with given generating function)
The second largest gay dating website in the world was exposed, and the status of programmers in 2022
An academic paper sharing and approval system based on PHP for computer graduation design
The vscode code is automatically modified to a compliance code when it is formatted and saved
How to draw non overlapping bubble chart in MATLAB
微服务组件Sentinel控制台调用
win32:堆破壞的dump文件分析
Summary and Reflection on the third week of winter vacation
基于人脸识别的课堂考勤系统 tkinter+openpyxl+face_recognition