LeetCode-Solutions-in-Cpp17

Solutions to high-frequency interview questions of LeetCode in C++17, taking into account both efficiency and comprehensibility.


Project maintained by downdemo Hosted on GitHub Pages — Theme by mattgraham
先假设所有数为质数,之后标记不为质数的数

2 => 468... 不为质数
3 => 6912... 不为质数
4 已标记不为质数
5 => 101520... 不为质数
6 已标记不为质数
7 => 142128... 不为质数
8 已标记不为质数
9 已标记不为质数
10 已标记不为质数
11 => 223344... 不为质数
12 已标记不为质数
13 => 263942... 不为质数
14 已标记不为质数
15 已标记不为质数
16 已标记不为质数
17 => 345168... 不为质数
18 已标记不为质数
19 => 385778不为质数
20 已标记不为质数
... 
class Solution {
 public:
  int countPrimes(int n) {
    if (n <= 2) {
      return 0;
    }
    vector<bool> dp(n, true);
    int cnt = 0;
    for (int i = 2; i < n; ++i) {
      if (dp[i]) {
        ++cnt;
        for (int j = i + i; j < n; j += i) {  // 后续为该质数倍数的数均不为质数
          dp[j] = false;
        }
      }
    }
    return cnt;
  }
};
class Solution {
 public:
  int countPrimes(int n) {
    if (n <= 2) return 0;
    vector<bool> dp(n, true);
    dp[0] = false;
    dp[1] = false;
    for (int i = 2; i < sqrt(n); ++i) {
      if (dp[i]) {
        for (int j = i + i; j < n; j += i) {
          dp[j] = false;
        }
      }
    }
    return count(begin(dp), end(dp), true);
  }
};
标记当前数与所有已记录质数的乘积
若当前数能被相乘的质数整除时,标记该乘积后结束当前数的标记
这样,所有的数都只被标记一次

2 => 2 * 2 => 2 整除 2,结束
3 => 3 * 23 * 3 => 3 整除 3,结束
4 => 4 * 2 => 2 整除 4,结束
5 => 5 * 25 * 35 * 5 => 5 整除 5,结束
6 => 6 * 2 => 2 整除 6,结束
7 => 7 * 27 * 37 * 57 * 7 => 7 整除 7,结束
8 => 6 * 2 => 2 整除 8,结束
9 => 9 * 29 * 3 => 3 整除 9,结束
10 => 10 * 2 => 2 整除 10,结束
11 => * 235711(如果设置了上限是 100121 超出了上限,就没必要标记)
12 => * 2
13 => * 23571113
14 => * 2
15 => * 23
16 => * 2
17 => * 2357111317
18 => * 2
19 => * 235711131719
20 => * 2
... 
class Solution {
 public:
  int countPrimes(int n) {
    if (n <= 2) {
      return 0;
    }
    vector<bool> dp(n, true);
    vector<int> primes;
    for (int i = 2; i < n; ++i) {
      if (dp[i]) {
        primes.emplace_back(i);
      }
      for (int j = 0; j < size(primes) && i * primes[j] < n; ++j) {
        dp[i * primes[j]] = false;
        if (i % primes[j] == 0) {
          break;
        }
      }
    }
    return size(primes);
  }
};