当前位置:网站首页>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)
边栏推荐
- How to deploy applications on kubernetes cluster
- [combinatorics] generating function (positive integer splitting | unordered | ordered | allowed repetition | not allowed repetition | unordered not repeated splitting | unordered repeated splitting)
- English grammar_ Noun classification
- Codeforces Round #803 (Div. 2) C. 3SUM Closure
- 一入“远程”终不悔,几人欢喜几人愁。| 社区征文
- Redis core technology and practice - learning notes (VIII) sentinel cluster: sentinel hung up
- Solve the problem of inaccurate network traffic monitored by ZABBIX with SNMP
- Niuke monthly race 31 minus integer
- [enumeration] annoying frogs always step on my rice fields: (who is the most hateful? (POJ hundred practice 2812)
- How do microservices aggregate API documents? This wave of operation is too good
猜你喜欢
2022-2028 global physiotherapy clinic industry research and trend analysis report
2022-2028 global petroleum pipe joint industry research and trend analysis report
Research Report on market demand and investment planning for the development of China's office chair industry, 2022-2028
PHP MySQL inserts multiple pieces of data
[combinatorics] generating function (summation property)
Bidding procurement scheme management of Oracle project management system
微服务组件Sentinel控制台调用
[untitled]
Golang string (string) and byte array ([]byte) are converted to each other
基于人脸识别的课堂考勤系统 tkinter+openpyxl+face_recognition
随机推荐
[combinatorics] generating function (positive integer splitting | unordered non repeated splitting example)
Mathematical formula (test)
[combinatorics] generating function (positive integer splitting | repeated ordered splitting | non repeated ordered splitting | proof of the number of repeated ordered splitting schemes)
Kotlin的協程:上下文
MySQL has been stopped in the configuration interface during installation
Research on Swift
Module 9 operation
[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 (positive integer splitting | unordered | ordered | allowed repetition | not allowed repetition | unordered not repeated splitting | unordered repeated splitting)
Redis core technology and practice - learning notes (11): why not just string
[combinatorics] generating function (use generating function to solve the combination number of multiple sets R)
[combinatorics] generating function (summation property)
supervisor监控Gearman任务
Line by line explanation of yolox source code of anchor free series network (6) -- mixup data enhancement
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
[combinatorics] generating function (generating function application scenario | using generating function to solve recursive equation)
Redis core technology and practice - learning notes (VIII) sentinel cluster: sentinel hung up
(9) Opencv Canny edge detection
A. Odd Selection【BruteForce】
An academic paper sharing and approval system based on PHP for computer graduation design