Queston

204. Count Primes

Solution

题目大意:给一个数,求小于这个数的素数的个数

思路:初始化一个boolean数组,初始设置为true,先遍历将2的倍数设置为false,再遍历3并将3的倍数置为false...

Java实现:

此法超时

public int countPrimes(int n) {
// 2, 3, 5, 7
boolean[] candidate = new boolean[n];
Arrays.fill(candidate, Boolean.TRUE);
candidate[0] = false;
candidate[1] = false;
int count = 0;
for (int i=2; i<n; i++) {
if (candidate[i]) {
count++;
for (int j = i+1; j<n; j++) {
if (j%i==0) candidate[j] = false;
}
}
}
return count;
}

参考别人的:

public int countPrimes(int n) {
boolean[] notPrime = new boolean[n];
int count = 0;
for (int i = 2; i < n; i++) {
if (notPrime[i] == false) {
count++;
for (int j = 2; i*j < n; j++) {
notPrime[i*j] = true;
}
}
}
return count;
}

204. Count Primes - LeetCode的更多相关文章

  1. [leetcode] 204. Count Primes 统计小于非负整数n的素数的个数

    题目大意 https://leetcode.com/problems/count-primes/description/ 204. Count Primes Count the number of p ...

  2. leetcode 263. Ugly Number 、264. Ugly Number II 、313. Super Ugly Number 、204. Count Primes

    263. Ugly Number 注意:1.小于等于0都不属于丑数 2.while循环的判断不是num >= 0, 而是能被2 .3.5整除,即能被整除才去除这些数 class Solution ...

  3. 【刷题-LeetCode】204. Count Primes

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

  4. [LeetCode] 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 计数质数

    Description: Count the number of prime numbers less than a non-negative number, n click to show more ...

  6. 【LeetCode】204 - Count Primes

    Description:Count the number of prime numbers less than a non-negative number, n. Hint: Let's start ...

  7. Java [Leetcode 204]Count Primes

    题目描述: Description: Count the number of prime numbers less than a non-negative number, n. 解题思路: Let's ...

  8. LeetCode 204. Count Primes (质数的个数)

    Description: Count the number of prime numbers less than a non-negative number, n. 题目标签:Hash Table 题 ...

  9. 【LeetCode】 204. Count Primes 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 素数筛法 参考资料 日期 [LeetCode] 题目 ...

随机推荐

  1. 数据结构:DHUOJ 删除链表的顺数及倒数第N个节点

    删除链表的顺数及倒数第N个节点 作者: turbo时间限制: 1S章节: DS:数组和链表 题目描述: 可使用以下代码,完成其中的removeNth函数,其中形参head指向无头结点单链表,n为要删除 ...

  2. 03-三高-并行并发&服务内

          三高项目-服务内并发 cap:分布式系统的起点. 一致性,可用性,分区容错性. P:分区容错性.分区,容错. 因为有网络的8大谬误: 网络是可靠的. 没有延迟 带宽无限 网络安全 拓扑结构 ...

  3. 移动端调试工具weinre安装教程(java版)

    先申明:本安装教程是基于java的jdk安装的,经过测试可以正常使用,基于nodejs的安装,小喵鼓弄了好几天也没有成功,如果哪位童鞋基于nodejs安装成功了,请联系小喵,小喵在这里先谢谢你了! 好 ...

  4. 【uniapp 开发】字典工具类 ObjectUtil

    {__/} ( • - •) /つ寿司 你要不要? {__/} ( • - •) /つ草莓 你要不要? {__/} ( • - •) /つ披萨 你要不要? {__/} ( • - •) /つ桃子 你要 ...

  5. jq移动端图片预览 (fly-zomm-img.js)

    效果图: ===>==> 里面还与很多属性设置: index  关闭按钮等等 代码: //html-----------------------<div class="he ...

  6. 《头号玩家》AI电影调研报告(四)

    五. 现实中对于技术的实现 1.自由变身 电影中,主人公借助"魔幻眼镜"让自己快速变成另一个人的模样,现实中我们一样也能做到! 在今年游戏开发者大会(GDC)上,一个名为" ...

  7. php文件下载服务器代码

    事情的起因 额,平板想下载电脑上的pdf文件,我开启了web服务,局域网下的ipad访问该文件web路径会直接打开该pdf,而不是下载.于是本小白就折腾了一下. 源代码 <?php forceD ...

  8. 解决帝国CMS搜索页面模板不支持灵动标签和万能标签的方法

    1,打开 /e/search/result/index.php 文件 查找 require("../../class/connect.php"); require(".. ...

  9. js的split函数

    split() 方法用于把一个字符串分割成字符串数组. separator 必需.字符串或正则表达式,从该参数指定的地方分割 stringObject. ***如果把空字符串 ("" ...

  10. Prometheus介绍及docker安装方式

    一.介绍 Prometheus是主要基于Go编写,最初在SoundCloud上构建的开源系统监视和警报工具包,它启发于 Google 的 borgmon 监控系统,由工作在 SoundCloud 的 ...