[LeetCode]204. Count Primes

LeetCode 204. Count Primes 及判断一个数是否是质数的方法 LeetCode 204. Count Primes 题意:给出一个非负整数n,求小于n的质数的个数。 阅读详情

[LeetCode]204. Count Primes

题目描述

这里写图片描述

思路

类似动态规划,每次计算当前素数时,将不可能是素数的结果标记,之后遍历碰到这些结果直接跳过

代码

#include <iostream>
#include <vector>

using namespace std;

class Solution {
public:
    int countPrimes(int n) {
        if (n <= 2) return 0;
        vector<int> passed(n, 0);

        int sum = 1, upper = sqrt(n);
        for (int i = 3; i < n; i += 2) {
            if (!passed[i]) ++sum;
            if (i > upper) continue;
            for (int j = i * i; j < n; j += i) passed[j] = 1;
        }

        return sum;
    }
};


int main() {
    Solution s;

    cout << s.countPrimes(2) << endl;
    cout << s.countPrimes(3) << endl;
    cout << s.countPrimes(4) << endl;
    cout << s.countPrimes(6) << endl;

    system("pause");
    return 0;
}
LeetCode - 204. Count Primes 题目 LeetCode - 204. Count Primes 题目链接 https://leetcode.com/problems/count-primes/ 参考博客 https://www.cnblogs.com/grandyang/p/4462810.html 解题思路 自行设计的算法时间复杂度是n∗n\sqrt{n} * nn​∗n,但是运行超时,参考博客的算法复杂度分析知时间复杂度相同... 阅读详情

相关推荐

[leetcode] 204. Count Primes

Description Count the number of prime numbers less than a non-negative number, n. Example: Input: 10 Output: 4 Explanation: There are 4 prime numbers less than 10, they are 2, 3, 5, 7. 分析 题目的意思是:给你一个...

Learning from the mistakes 209

LeetCode204 Count Primes 详解!

关于求n以内的素数有很多种方法,如果要列出素数来的话我们尝试着把根号n以内的数除一遍就可以了,但是对于筛选法比如我们去掉2的倍数,3的倍数…等等也是极为巧妙的。LeetCode204这道题目所要的是不大于n的素数个数,这里看完讨论区的大神代码——我们计算个数的时候使用筛选法并且可以不用遍历整个数组,使我受益匪浅,但是美中不足的是所给代码并没有注释,刚开始看的时候也是极其懵逼,后面耐心的理解并添加...

ni_new_sm_new的博客 274

Leetcode 204.Count Primes

Leetcode 204. Count Primes Count the number of prime numbers less than a non-negative number, n. 知识:任意一个合数都可以进行质因数分解,即任意一个合数都是一个小于自己的素数的倍数。 一、试除法 进行打表,一个数n如果是比它小的任意一个素数的倍数,则它是合数。 复杂度O(...

yangchz的博客 393

leetcode刷题记录-204. Count Primes

leetcode刷题记录-204. Count Primes

zhaizhan501的博客 216

面试题素数问题 && Leetcode 204. Count Primes

找到一篇简单粗暴的介绍面试质素算法的文章。 然后就涉及到如何按位保存。 在C++里,要按照bit来存储可以使用容器bitset。但是需要预先指定长度,例如bitset bar。 要动态分配长度,可以使用vector,它会优化,长度可能为1bit。但是实际操作中发现不好用,不能与bool类型的值判等,也不能赋值(可能是我没搞懂,但在微软对该类的介绍页面也没看到相关用法,只有用cout输

GTMer的专栏 257

Leetcode204-Count Primes

这个题相当于求小于n的所有质数,曾在编程之美中看到过一个求一组质数的算法,叫厄拉多塞筛法,时间复杂度仅有O(nloglogn),这是一个相当好的算法(如果从1到n-1分别判断质数,时间复杂度为O(n*sqrt(n)))。 厄拉多塞筛法的步骤:建立从2到n的集合G={2, 3, 4, ..., n},每次从集合中取出最小的数A,这个数就是质数;然后将数A * times从集合中删除,其中1<=times<=n/A。得到一个新的集合G',重复上述步骤直到集合为空,就取出了所有质数。

coderhuhy的专栏 913

LeetCode 204: Count Primes

LeetCode 204: Count Primes 题目描述 给定一个非负整数,计算小于该整数的所有素数(最小的素数为2)。 解题思路 该题的解法有一个特定的名字:埃拉托斯特尼筛法,下面简单说一下,具体可以参阅网上的资料。 判断一个数x是否是合数,只要依次除以2至sqrt(x)间的素数,判断是否整除即可。 埃拉托斯特尼筛法基于以下原理,给定一个素数n>1,kn是一个合数(k&g...

HakunaMatata的博客 163

Leetcode204. Count Primes

题目地址: https://leetcode.com/problems/count-primes/ 给定nnn非负,问小于nnn的素数个数。 法1:埃拉托斯特尼筛法(Sieve of Eratosthenes)。开一个长度为nnn的boolean数组记录每个数是否是素数。首先全填充为true,将000和111置为false。接着开始找第一个是true的数,是222,接着把222的比自己大的倍数全标记为false;接着再向后找是true的数,然后重复上面的操作。这样小于nnn的所有非素数就全被标记为false

数学、算法爱好者的博客 257

Leetcode_204_Count Primes

本文是在学习中的总结,欢迎转载但请注明出处:http://blog.csdn.net/pistolove/article/details/46366207 Description: Count the number of prime numbers less than a non-negative number, n. 思路: (1)题意为给定整数n,求解n以内的整数中有多少个素数(质数)。 (2)该题涉及到数学相关的知识。首先,看下素数的定义:质数(prime

皮斯特劳沃 1345

LeetCode 204:Count Primes

Description: Count the number of prime numbers less than a non-negative number, n 分析: 题目要求计算小于N的所有素数的个数。所有主程序很简单:判断是否是素数字,如是,count++ (暂且忽略prim_vec, 后面判断素数时会用到): int countPrimes(int n) {

哈哈的个人专栏 4170

LeetCode 204 Count Primes

题目描述Description:Count the number of prime numbers less than a non-negative number, n.Credits: Special thanks to @mithmatt for adding this problem and creating all test cases.分析这道题,直接看hint即可!Hint: Let’

Yano_nankai的博客 587

LeetCode204——Count Primes

Description: Count the number of prime numbers less than a non-negative number, n. Credits: Special thanks to @mithmatt for adding this problem and creating all test cases. 实现: int countPrim

代码菌的blog 1635

leetcode-204-Count Primes

问题题目:[leetcode-204]思路素数筛法。 方法不难,但是搞了我很多时间。首先不用全局或者静态变量,在堆上开辟是一样的。 用vector就好。之前做acm的时候,用的全局初始化。 然后后面每次判断就很容易。但是,用c++写的时候,由于没用全局或者静态变量。 所以,在写的时候。我一直有个误区:单次觉得没有初始化的必要。 一直纠结在怎么样全局变量和静态变量上。其实,单次跑的时候,也有

Kang_TJU的博客 588

leetcode 204: Count Primes

解题思路: 质数是只能被1和它本身整除的数。 质数2: 2 * 2, 2 * 3, 2 * 4, 2 * 5, 2 * 6, 2 * 7, … 2 * (n / 2)都不是质数 质数3 :3 * 2, 3 * 3, 3 * 4, 3 * 5, 3 * 6, 3 * 7, … 3 * (n / 3)都不是质数 3 * 2 在质数2时考虑过了,直接从3 * 3考虑即可 合数4 :因为4为

永远不要给自己找借口 368

Leetcode 204 Count Primes

Description: Count the number of prime numbers less than a non-negative number, n. 统计小于n的素数有多少个。 用筛法进行素数打表,边打表边记录个数。 class Solution { public: int countPrimes(int n) { vector mp(n, 0)

无名山丘,崛起成峰 1389

leetcode】【204Count Primes

一、问题描述 Description: Count the number of prime numbers less than a non-negative number, n. Credits: Special thanks to @mithmatt for adding this problem and creating all test cases. Hint:

斗code、年华 422

LeetCode204 题 (Count Primes)

LeetCode204 题 (Count Primes) Description: Count the number of prime numbers less than a non-negative number, n. 计算小于 N 的素数的个数。这道题目比较简单。但是想提高计算效率与需要费点脑筋。判断一个数字 nn 是不是素数的简单方法是 用 nn 去除 2,3,4,…,n−1

Ivan 的专栏 2190

[leetcode]204. Count Primes

题目链接:https://leetcode.com/problems/count-primes/ Description: Count the number of prime numbers less than a non-negative number,n. class Solution{ public: vector prim_vec; bool isPri

xiaocong1990的博客 288
上一篇: [LeetCode]537. Complex Number Multiplication
下一篇: [LeetCode]624. Maximum Distance in Arrays
charon____
博客等级 码龄11年 3粉丝 202原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值