LeetCode204. Count Primes

文章目录

一、题目

Given an integer n, return the number of prime numbers that are strictly less than n.

Example 1:

Input: n = 10
Output: 4
Explanation: There are 4 prime numbers less than 10, they are 2, 3, 5, 7.
Example 2:

Input: n = 0
Output: 0
Example 3:

Input: n = 1
Output: 0

Constraints:

0 <= n <= 5 * 106

二、题解

class Solution {
public:
    int countPrimes(int n) {
        int res = 0;
        vector<bool> sign(n,false);
        for(int i = 2;i < n;i++){
            if(!sign[i]){
                res++;
                for(int j = i + i;j < n;j += i) sign[j] = true;
            }
        }
        return res;
    }
};

相关推荐

  1. Leetcode274

    2024-03-20 19:48:03       13 阅读
  2. LeetCode204. Count Primes

    2024-03-20 19:48:03       22 阅读
  3. Leetcode204.计数质数

    2024-03-20 19:48:03       12 阅读
  4. LeeetCode 206

    2024-03-20 19:48:03       29 阅读
  5. leetcode 24

    2024-03-20 19:48:03       28 阅读
  6. LeetCode202. 快乐数

    2024-03-20 19:48:03       34 阅读
  7. LeetCode 224:基本计算器

    2024-03-20 19:48:03       40 阅读
  8. leetcode-200-岛屿问题

    2024-03-20 19:48:03       40 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-03-20 19:48:03       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-03-20 19:48:03       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-03-20 19:48:03       19 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-03-20 19:48:03       20 阅读

热门阅读

  1. 描述一下使用过的任何安全测试工具及其功能

    2024-03-20 19:48:03       24 阅读
  2. 分布式事务的实现方式

    2024-03-20 19:48:03       23 阅读
  3. C++ 函数模板

    2024-03-20 19:48:03       20 阅读
  4. 机器学习模型—K means

    2024-03-20 19:48:03       20 阅读
  5. 实验11-1-9 藏尾诗(PTA)

    2024-03-20 19:48:03       21 阅读
  6. 单片机实践:开发板上运行AES128防盗算法

    2024-03-20 19:48:03       15 阅读
  7. MATLAB是什么,它主要用于什么?

    2024-03-20 19:48:03       21 阅读
  8. 算法体系-12 第 十二 二叉树的基本算法

    2024-03-20 19:48:03       17 阅读