Count Primes

Description:

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

click to show more hints.

Credits:
Special thanks to @mithmatt for adding this problem and creating all test cases.

Have you met this question in a real interview?
 
class Solution
{
public:
int countPrimes(int n)
{
vector<int> a(n);
for(int i=; i<n; i++)
a[i] = i; for(int i=; i * <= n; i++)
{
if(i == )
{
a[i] = ;
continue;
} for(int j = i + i; j < n; j += i)
{
if(a[j] != )
a[j] = ;
}
} int count = ;
for(int i=; i<n; i++)
{
cout << a[i] << " ";
if(a[i] != )
{
count++;
}
}
cout << endl; return count;
}
};

[leetcode] Count Primes的更多相关文章

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

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

  2. Python3解leetcode Count Primes

    问题描述: Count the number of prime numbers less than a non-negative number, n. Example: Input: 10 Outpu ...

  3. LeetCode Count Primes 求素数个数(埃拉托色尼筛选法)

    题意:给一个数n,返回小于n的素数个数. 思路:设数字 k =from 2 to sqrt(n),那么对于每个k,从k2开始,在[2,n)范围内只要是k的倍数的都删掉(也就是说[k,k2)是不用理的, ...

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

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

  5. 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 ...

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

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

  7. 204. Count Primes - LeetCode

    Queston 204. Count Primes Solution 题目大意:给一个数,求小于这个数的素数的个数 思路:初始化一个boolean数组,初始设置为true,先遍历将2的倍数设置为fal ...

  8. LeetCode_204. Count Primes

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

  9. HDU 5901 Count primes 论文题

    Count primes 题目连接: http://acm.split.hdu.edu.cn/showproblem.php?pid=5901 Description Easy question! C ...

随机推荐

  1. 安装虚拟机VMware tools

    不懂得安装虚拟机VMware tools的想必都是刚在虚拟机上玩系统初学者,无疑我们对虚拟机的了解并不深,这使得本来很容易安装的VMware tools在我们安装时变得复杂而又难以琢磨,到头一直的付出 ...

  2. iOS8 VPN 应用内连接

    iOS8 开放了关于VPN的API,开发者能够在应用中创建VPN配置,并控制VPN的连接.不过只支持了IPSec和IKEv2两种协议. 关于这方面资料,很少,在这里要感谢一下 今晚打老虎,是在他得帮助 ...

  3. 【深入浅出.Net IL】1.一个For循环引发的IL

    .Net底层剖析目录章节 1.[深入浅出.Net IL]1.一个For循环引发的IL 2.[.Net底层剖析]2.stfld指令-给对象的字段赋值 3.[.Net底层剖析]3.用IL来理解属性 1.准 ...

  4. php -- php读取sqlserver2005的数据实现分页查询

    --php5.2 --sqlserver2005 php读取sqlserver多条数据时,有时因为数据太多,需要进行分页查询. 例如,按价格的顺序,读取同一类型的产品的第30到第48条记录 $star ...

  5. ubuntu14.04编译安装Git2.7

    在开源中国看文章, 随意之间, 在软件资讯栏看到git 2.7的信息. 一直在使用在git 1.9.1, 心中突感, 这个git 2.7是个什么东西, 怎么git的版本更新有如此快么. 印象里, 老外 ...

  6. Odyssey.js – 创建互动的,有吸引力的故事

    Odyssey.js 是一个开源工具,它可以让你的地图,叙述和其他多媒体结合成一个美丽的故事.创建新的故事很简单,要求无非是一个现代的 Web 浏览器和一个好的想法.你可以使用现成的模板来控制和设计精 ...

  7. Linq专题之Linq查询from子句

    Linq查询表达式包含8个常用的子句:from.where.select.join.into.orderby.group.let.我们来看看详细的说明.      from:      指定查询操作的 ...

  8. TCP和UDP之间的区别

    TCP---传输控制协议,提供的是面向连接.可靠的字节流服务.当客户和服务器彼此交换数据前,必须先在双方之间建立一个TCP连接,之后才能传输数据.TCP提供超时重发,丢弃重复数据,检验数据,流量控制等 ...

  9. c# 正则表达式 匹配中括号&颜色过滤

    现在需要匹配 [color=#000000],以"[color"开头,以"[/color]"结束,中间字符数量不限制,最后返回所有匹配的下标. 代码如下: // ...

  10. ActiveReports 9 新功能:创新的报表分层设计理念

    在最新发布的ActiveReports 9报表控件中添加了多项新功能,以帮助你在更短的时间里创建外观绚丽.功能强大的报表系统,本文将重点介绍创新的报表分层设计理念,对报表内容进行分组管理与设计,易于实 ...