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.

题目大意

  求所有小于非负整数n的质数的数量。

理  解:

  最直接的做法就是求每个数i 是否有2到 i-1间的因子。统计质数个数。但该方法时间复杂度太高,会超出时间限制。

  所有的非质数都含有质数因子,每找到一个质数变存入容器中,判断一个数i是否为质数时判断容器中元素是否为它的因子,没找到则该数为质数,也加入容器,但这个时间和空间复杂度也比较高。

  看了其他人的方法,厄拉多塞筛法,所有元素都设置为未访问状态,每访问一个状态为true的元素,将它的倍数都设置为false。最终得到的为true的元素即为质子。

代 码 C++:

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

运行结果:

    执行用时 :100 ms, 在所有C++提交中击败了32.49%的用户

内存消耗 :8.5 MB, 在所有C++提交中击败了76.65%的用户

转载于:https://www.cnblogs.com/lpomeloz/p/11015034.html

letecode [204] - Count Primes相关推荐

  1. leetCode 204. Count Primes 哈希 求素数

    204. Count Primes 求素数 Description: Count the number of prime numbers less than a non-negative number ...

  2. 【LeetCode 剑指offer刷题】特殊数题3:204 Count Primes

    [LeetCode & 剑指offer 刷题笔记]目录(持续更新中...) 204. Count Primes Count the number of prime numbers less t ...

  3. [LeetCode] 204. Count Primes

    204. Count Primes Count the number of prime numbers less than a non-negative number, n. Example: Inp ...

  4. 204. Count Primes

    Count the number of prime numbers less than a non-negative number, n. Example: Input: 10 Output: 4 E ...

  5. LeetCode 204. Count Primes

    注:质数从2开始,2.3-- 改进过程: 一.常规思路是对小于n的每一个数进行isPrime判断,isPrime(int x)函数中for(int i = 2; i <= x /2; ++i), ...

  6. leetcode python3 简单题204. Count Primes

    1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第二百零四题 (1)题目 英文: Count the number of prime ...

  7. LeetCode -- 204. Count Primes

    题目标签 HashTab(哈希表) 题意及思路 题意:略 思路:有关素数的题目我所知道有两种做法.一种是最基本的isPrime算法,关键点在循环判断时,上限为Math.sqrt(n) (求n是否为素数 ...

  8. Leetcode-204 Count Primes

    #204 Count Primes Count the number of prime numbers less than a non-negative number, n. 题解:这道题如果对每个小 ...

  9. LeetCode 204. Count Primes--从一开始的质数个数--Python解法--面试算法题

    题目地址:Count Primes - LeetCode Count the number of prime numbers less than a non-negative number, n. E ...

最新文章

  1. shell执行mysql命令
  2. 快速了解微信小程序的使用,一个根据小程序的框架开发的todos app
  3. java导出csv文件使用Excel打开乱码问题
  4. PHP pear安装
  5. 《啊哈!算法》笔记_Day03
  6. 1.15 Java访问控制修饰符(public、 private、protected 和 friendly)
  7. mfp 服务器控制中心,小身材大作用 固网USB打印服务器评测
  8. JAVA面试常考系列一
  9. 阿里云容器服务新增支持Kubernetes编排系统,性能重大提升 1
  10. github 思维导图开元软件_Windows上有哪些很棒思维导图的软件
  11. 【备忘】二叉树遍历的迭代实现
  12. (day 42 - 字符翻转 ) 剑指 Offer 58 - II. 左旋转字符串
  13. linux的df命令根目录,详细分析Linux df命令的使用方法
  14. 模拟人生 4:如何在游戏中生成收藏品
  15. Linux使用ragel进行文本快速解析(上)
  16. 2021年关于Delphi/Object Pascal编程语言的现状和历史
  17. G.652与G.655单模光纤分类及对比
  18. python+selenium自动截取政府官网及失信公示平台搜索结果图
  19. PLC可编程控制器实验装置
  20. TP5 生成二维码并合成图片

热门文章

  1. 医疗设备-监护仪-数据解析(协议、通信)
  2. java实现翻转二叉树_【leetcode刷题】[简单]226. 翻转二叉树(invert binary tree)-java...
  3. python 数据结构定义_【Python】数据结构如何定义以及清理 例如订单的附带商品信息数据...
  4. 【OpenCV】OpenCV函数精讲之 -- 通道分离:split()函数
  5. 超赞Transformer+CNN=SOTA!
  6. Pytorch还是TensorFlow?顶会带你览趋势
  7. 资源下载| 机器学习经典书籍《统计学习方法》(Python3.6)代码实现(及课件)
  8. Hi,我们是-MobileNet-家族
  9. 深度学习(三十八)卷积神经网络入门学习(2.0)
  10. python socket监听端口_Python 用socket模块实现检测端口和检测web服务