update code2

This commit is contained in:
ShusenTang 2020-07-04 20:04:42 +08:00 committed by GitHub
parent 8de5761fc3
commit d75905bfbe
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -37,14 +37,15 @@ public:
class Solution { class Solution {
public: public:
int countPrimes(int n) { int countPrimes(int n) {
vector<unsigned int>nums(n, 1); // 0代表被划去1代表没被划去 vector<bool>Prime(n + 1, true);
int count = 0; int res = 0;
for(int i = 2; i < n; i++){ for(int i = 2; i < n; i++){
if(nums[i] == 0) continue; if(Prime[i]){
count++; res++;
for(int j = 2; j * i < n; j++) nums[j * i] = 0; for(int j = i; j <= n / i; j++) Prime[i*j] = false;
}
} }
return count; return res;
} }
}; };
``` ```