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

计算小于n的质数的个数,当然就要用到大名鼎鼎的筛法了,代码如下,写的有点乱不好意思。

 class Solution {
public:
int countPrimes(int n) {
vector<int> vtor(n + , );
vector<int> ret;
for (int i = ; i <= n; ++i)
vtor[i] = i;
for (int i = ; i < n; ++i){//边界条件注意
if (vtor[i] != -){
ret.push_back(vtor[i]);
int tmpPrime = vtor[i];
for (int mul = ; tmpPrime * mul <= n; mul++){
vtor[tmpPrime * mul] = -;
}
}
}
return ret.size();
}
};

java版本的代码如下所示:

public class Solution {
public int countPrimes(int n) {
int [] prime = new int[n+]; //这里其实用boolean更好,懒得改了
int count = ;
for(int i = ; i < n+; ++i){
prime[i] = i;
}
for(int i = ; i < n+; ++i){
if(prime[i] == -)
continue;
else{
count++;
for(int mul = ; mul * i < n+; ++mul){
prime[mul*i] = -;
}
}
}
return count;
}
}

LeetCode OJ:Count Primes(质数计数)的更多相关文章

  1. [LeetCode] 204. Count Primes 质数的个数

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

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

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

  3. [LeetCode]Count and Say 计数和发言

    Count and Say 计数和发言 思路:首先要理解题意,可以发现后者是在前者的基础之上进行的操作,所以我们拿之前的结果作为现在函数的参数循环n-1次即可,接下来就是统计字符串中相应字符的个数,需 ...

  4. [LeetCode] 204. Count Primes 计数质数

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

  5. LeetCode 204. Count Primes计数质数 (C++)

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

  6. [LeetCode] Count Primes 质数的个数

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

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

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

  8. LeetCode 204 Count Primes

    Problem: Count the number of prime numbers less than a non-negative number, n. Summary: 判断小于某非负数n的质数 ...

  9. 【leetcode】Count Primes(easy)

    Count the number of prime numbers less than a non-negative number, n 思路:数质数的个数 开始写了个蛮力的,存储已有质数,判断新数字 ...

  10. leetcode:Count Primes

    Description:Count the number of prime numbers less than a non-negative number, n. 本题给定一个非负数n,让我们求小于n ...

随机推荐

  1. 利用VMware克隆linux虚拟机需要注意的事项

    利用VMware克隆虚拟机需要注意的问题 2018年03月30日 18:20:29 温文尔雅的流氓 阅读数:1343更多 个人分类: linux   版权声明:本文为博主原创文章,未经博主允许不得转载 ...

  2. [动态规划]UVA437 - The Tower of Babylon

     The Tower of Babylon  Perhaps you have heard of the legend of the Tower of Babylon. Nowadays many d ...

  3. C# 调用win api获取chrome浏览器中地址

    //FindWindow 查找窗口 //FindWindowEx查找子窗口 //EnumWindows列举屏幕上的所有顶层窗口,如果回调函数成功则返回非零,失败则返回零 //GetWindowText ...

  4. python中unicode和str的组合

    python中unicode对象和str对象拼接在一起,会自动将str对象转换成unicode对象 即:a="aa" b=u"bb" c=a+b type(c) ...

  5. beego——过滤器

    beego支持自定义过滤中间件,例如安全验证.强制跳转等. 过滤器函数如下所示: beego.InsertFilter(pattern string, position int, filter Fil ...

  6. HDU 1501 Zipper 【DFS+剪枝】

    HDU 1501 Zipper [DFS+剪枝] Problem Description Given three strings, you are to determine whether the t ...

  7. RAID详解[RAID0/RAID1/RAID5]

    RAID(Redundant Array of Independent Disk 独立冗余磁盘阵列)技术是加州大学伯克利分校1987年提出,最初是为了组合小的廉价磁盘来代替大的昂贵磁盘,同时希望磁盘失 ...

  8. Go语言学习之常量(The way to go)

    生命不止,继续go go go . 上一篇博客<Go语言学习之变量(The way to go)介绍了go中的变量,今天就介绍常量. const关键字 跟c++中一样,go中同样具有const关 ...

  9. maven说明

    1.maven 仓库地址 http://mvnrepository.com/ 2.maven jar包搜索地址 http://search.maven.org/ 3. 点开上面的 版本链接,就可以看到 ...

  10. Sharding-Jdbc实现分表分库

    Sharding-Jdbc分表分库LogicTable数据分片的逻辑表,对于水平拆分的数据库(表),同一类表的总称.订单信息表拆分为2张表,分别是t_order_0.t_order_1,他们的逻辑表名 ...