Description:

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

题意:给出一个数n,求小于n的素数的个数

思路:用 Sieve of Eratosthenes

代码如下:

class Solution
{public int countPrimes(int n){boolean[] isPrime = new boolean[n];for (int i = 2; i < n; i++){isPrime[i] = true;}int m = (int)Math.sqrt(n);for (int i = 2; i <= m; i++){if (!isPrime[i]) continue;for (int j = i * i; j < n; j += i){isPrime[j] = false;}}int count = 0;for (int i = 2; i < n; i++){if (isPrime[i]) count++;}return count;}
}

LeetCode Count Primes相关推荐

  1. [LeetCode] Count Primes

    Description: Count the number of prime numbers less than a non-negative number, n 解题思路 采用Eratosthene ...

  2. [LeetCode] Count Primes - 素数系列问题

    题目概述: Description: Count the number of prime numbers less than a non-negative number, n. 解题方法: 题意是给出 ...

  3. LeetCode:Count Primes

    Problem: Description: Count the number of prime numbers less than a non-negative number, n. Credits: ...

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

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

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

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

  6. [LeetCode] 204. Count Primes

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

  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-204 Count Primes

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

  9. LeetCode Count Complete Tree Nodes(二分法)

    问题:给出一个完全二叉树,求其结点个数 思路:第一种方式时直接使用递归法,将其左子树个数加上右子树个数再加上根结点 第二种方式二分法,因为完全二叉树除了最后一层外,其它都满足有2^i个结点,而最后一层 ...

最新文章

  1. 1.html5究竟是什么
  2. Ubuntu使用tzselect修改时区
  3. 怎么查看python是否安装成功-如何查看python是否安装成功?
  4. 深究 ElasticSearch 查询的秘密
  5. 第二章 PX4-RCS启动文件解析
  6. java调度:(六)quarts_cron表达式
  7. FactoryBean 源码
  8. Apache Shiro源码解读之SecurityManager的创建
  9. android http 慢,android httpurlconnection數據連接速度慢
  10. make_smbcodepage - 为Samba创建代码页文件
  11. hbase记录日志wal_SQL Server事务日志–第1部分–日志结构和预写日志记录(WAL)算法
  12. 无人车企哪家强?通用第一Waymo第二,苹果特斯拉进黑榜
  13. UEditor DIY笔记
  14. 递归算法在生成树型结构中,几乎完全属于无稽的算法
  15. 编程也讲禅,您读过《金刚经》吗?——ADO.NET核心类的灭度与SQLHelper的诞生——十八相送(上)...
  16. 五子棋游戏代码(完整版)
  17. 【LaTex】LaTex的下载与安装(超详细、超简洁)
  18. 五分钟了解设计模式六大原则(上)
  19. Java实现模拟斗地主洗牌、发牌、看牌并排序
  20. 交易员都是用什么方法盈利的?

热门文章

  1. Gson源码分析之Json结构抽象和注解使用
  2. 第08次:升级《陋习手记》完善主从UI
  3. 【设计原则和建议】 构造和析构对象
  4. python可以从事什么工作-对话极客晨星:现在学Python 长大可以从事什么工作?...
  5. 没有任何基础的可以学python吗-对没有编程基础的人来说,直接学Python入门IT合适吗?...
  6. python中turtle画酷炫图案-用python打造可视化爬虫监控系统,酷炫的图形化界面
  7. 初学python的体会心得-python初学心得
  8. python excel 自动化-Python控制Excel实现自动化办公
  9. python程序员需要掌握哪些技术-程序员Python编程必备5大工具,你用过几个?
  10. 自学python方法-从零开始自学python编程,你找对方法了吗?