[leetcode] Count Primes
Count Primes
Description:
Count the number of prime numbers less than a non-negative number, n
Credits:
Special thanks to @mithmatt for adding this problem and creating all test cases.
- 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的更多相关文章
- [LeetCode] Count Primes 质数的个数
Description: Count the number of prime numbers less than a non-negative number, n click to show more ...
- Python3解leetcode Count Primes
问题描述: Count the number of prime numbers less than a non-negative number, n. Example: Input: 10 Outpu ...
- LeetCode Count Primes 求素数个数(埃拉托色尼筛选法)
题意:给一个数n,返回小于n的素数个数. 思路:设数字 k =from 2 to sqrt(n),那么对于每个k,从k2开始,在[2,n)范围内只要是k的倍数的都删掉(也就是说[k,k2)是不用理的, ...
- [leetcode] 204. Count Primes 统计小于非负整数n的素数的个数
题目大意 https://leetcode.com/problems/count-primes/description/ 204. Count Primes Count the number of p ...
- 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 ...
- 【刷题-LeetCode】204. Count Primes
Count Primes Count the number of prime numbers less than a non-negative number, *n*. Example: Input: ...
- 204. Count Primes - LeetCode
Queston 204. Count Primes Solution 题目大意:给一个数,求小于这个数的素数的个数 思路:初始化一个boolean数组,初始设置为true,先遍历将2的倍数设置为fal ...
- LeetCode_204. Count Primes
204. Count Primes Easy Count the number of prime numbers less than a non-negative number, n. Example ...
- HDU 5901 Count primes 论文题
Count primes 题目连接: http://acm.split.hdu.edu.cn/showproblem.php?pid=5901 Description Easy question! C ...
随机推荐
- 审核流(3)低调奢华,简单不凡,实例演示-SNF.WorkFlow--SNF快速开发平台3.1
下面我们就从什么都没有,结合审核流进行演示实例.从无到有如何快速完美的实现,然而如此简单.低调而奢华,简单而不凡. 从只有数据表通过SNF.CodeGenerator代码生成器快速生成单据并与审核流进 ...
- 【MVC 过滤器的应用】ASP.NET MVC 如何统计 Action 方法的执行时间
代码如下: using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; u ...
- Android之startActivityForResult的使用
在Android中startActivityForResult主要作用就是: A-Activity需要在B-Activtiy中执行一些数据操作,而B-Activity又要将,执行操作数据的结果返回给A ...
- IE下angularJS页面跳转的bug
用Angularjs做项目的过程中遇到一种情况:就是在IE浏览器下,当访问网站页面后,点击浏览器中的向左和向右(返回和前进)按钮时,需要点击两次才能正确跳转,但是在chrome及其他浏览器下该bug没 ...
- C#将Json字符串反序列化成List对象类集合
摘自:http://blog.csdn.net/cdefg198/article/details/7520398 using System.IO; using System.Web.Script.Se ...
- [git]查看某一行代码是谁写的
git blame filename,'blame'意思为责怪!哈哈哈. 就会列出来每行的修改纪录.你可以通过行数或者代码来查看,是谁的锅!
- Android 学习笔记之数据存储SharePreferenced+File
学习内容: Android的数据存储.... 1.使用SharedPreferences来保存和读取数据... 2.使用File中的I/O来完成对数据的存储和读取... 一个应用程序,经常需要与用 ...
- IE11之F12 Developer Tools--控制台工具(Console)
前面我们介绍了IE11的Developer Tools中的第一个工具--DOM Explorer,这篇文章介绍第二个工具--控制台(Console),使用控制台工具查看错误和其他信息.发送调试输出.检 ...
- Ubuntu13.04配置:Vim+Syntastic+Vundle+YouCompleteMe
序言 使用Ubuntu和vim已经有一段时间了,对于Vim下的插件应用,我总是抱着一股狂热的态度.这次,又在网上闲逛着,发现了一个个人博客提到了Vim代码补全这回事,并提到了YouCompleteMe ...
- 剑指offer面试题31连续子数组的最大和
一.题目描述 HZ偶尔会拿些专业问题来忽悠那些非计算机专业的同学.今天测试组开完会后,他又发话了:在古老的一维模式识别中,常常需要计算连续子向量的最大和,当向量全为正数的时候,问题很好解决.但是,如果 ...