#204 Count Primes

Count the number of prime numbers less than a non-negative number, n.

题解:这道题如果对每个小于n的数都进行判断是否为素数并计数会超时,因此采用筛法来解这题。建一个数组,从2开始, 把其倍数小于N的都删掉。

class Solution {
public:int countPrimes(int n) {vector<int>arr(n,1);int sum=0;for(int i=2;i<=n;i++){if(arr[i]==1){sum++;for(int j=i;j<n;j+=i){arr[j]=0;}}}return sum;}
};

转载于:https://www.cnblogs.com/fengxw/p/6061597.html

Leetcode-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] 204. Count Primes

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

  3. LeetCode 204. Count Primes

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

  4. LeetCode -- 204. Count Primes

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

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

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

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

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

  7. 204. Count Primes

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

  8. leetcode python3 简单题204. Count Primes

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

  9. letecode [204] - Count Primes

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

最新文章

  1. python文本菜单程序_python3.x Day1 菜单程序练习
  2. 数据结构----数组与广义表专题
  3. 如何获取上传文件的本地路径
  4. EditPlus 技巧大全:[1]怎么配置PHP编译环境
  5. composer的使用
  6. Python+matplotlib设置y轴标签距离、位置、竖排
  7. eclipse新建tomcat server但是总是报404的解决方法
  8. no cortex-m sw device found_SW大模块水箱桂林厂家图纸
  9. 转换运行时获取DTP语义组
  10. 【CentOS7】服务环境搭建
  11. 使用数字全通滤波器对IIR滤波器进行相位补偿
  12. MATLAB身份证号码识别系统(GUI论文)
  13. lwip---(五)以太网数据接收
  14. 【208.11.21 直播QA】TI C2000 Piccolo单芯片——实现双轴伺服电机和马达控制
  15. rundll32.exe传入参数
  16. pip:Could not fetch URL ***: There was a problem confirming the ssl certificate: HTTPSConnectionPool
  17. CYUSB3014芯片使用EEPROM无法下载固件说明
  18. 机械手表,石英手表,智能手表怎么选最好,哪种更适合佩戴?
  19. 个人看过较好的电影推荐
  20. windows使用ssh连接远程服务器

热门文章

  1. 2018值得一看的GAN论文回顾
  2. 请不要把数据分析和机器学习混为一谈
  3. 关于Arcgis工作空间的记忆点
  4. java thread start0_Java: Thread类中start()和run()的区别
  5. 100级大橙武升级流程_DNF:女气功升级100级无暇手套,前后伤害对比。
  6. 小红书去水印代码_小红书商家须知!小红书引流靠谱吗?
  7. 微信小程序中 button丢失其默认样式
  8. sql 数据检索后的替换格式化
  9. 前端异步编程之Promise和async的用法
  10. Python常用数据结构之heapq模块