Leetcode Count Prime
Description:
Count the number of prime numbers less than a non-negative number, n
Hint: The number n could be in the order of 100,000 to 5,000,000.
#define NO 0
#define YES 1 class Solution {
public:
int countPrimes(int n) {
if(n == 1)
return 0; int num;
int count = 0;
for(num = 2;num <= n;num++){
if(isPrime(num))
count++;
} return count;
} int isPrime(int n){
int i = 2;
for(;i<=sqrt(n);i++)
{
if( (n%i) == 0)
return NO;
} return YES;
} };
Leetcode Count Prime的更多相关文章
- [LeetCode] Count Primes 质数的个数
Description: Count the number of prime numbers less than a non-negative number, n click to show more ...
- [LeetCode] Count Numbers with Unique Digits 计算各位不相同的数字个数
Given a non-negative integer n, count all numbers with unique digits, x, where 0 ≤ x < 10n. Examp ...
- [LeetCode] Count of Range Sum 区间和计数
Given an integer array nums, return the number of range sums that lie in [lower, upper] inclusive.Ra ...
- [LeetCode] Count of Smaller Numbers After Self 计算后面较小数字的个数
You are given an integer array nums and you have to return a new counts array. The counts array has ...
- [LeetCode] Count Univalue Subtrees 计数相同值子树的个数
Given a binary tree, count the number of uni-value subtrees. A Uni-value subtree means all nodes of ...
- [LeetCode] Count Complete Tree Nodes 求完全二叉树的节点个数
Given a complete binary tree, count the number of nodes. Definition of a complete binary tree from W ...
- LeetCode Count of Range Sum
原题链接在这里:https://leetcode.com/problems/count-of-range-sum/ 题目: Given an integer array nums, return th ...
- LeetCode Count of Smaller Numbers After Self
原题链接在这里:https://leetcode.com/problems/count-of-smaller-numbers-after-self/ 题目: You are given an inte ...
- [leetcode] Count Primes
Count Primes Description: Count the number of prime numbers less than a non-negative number, n click ...
随机推荐
- 【解决】python2.x版本的Django下admin管理页面css无效
折腾一下午,终于解决了这个问题,有必要记录一下,我就奇怪了为什么实验室电脑没问题,到宿舍就挂掉了,哼 主要是改mimetypes文件,位于D:\MySoftware\Python27\Lib下 1.添 ...
- JS自定义事件(Dom3级事件下)
原文出处: http://www.w3cfuns.com/notes/11861/e21736a0b15bceca0dc7f76d77c2fb5a.html . 我拿出作者中的一段,感谢作者原创. ...
- 横竖屏事件响应(viewWillLayoutSubviews和通知)两种方式
转载:http://blog.csdn.net/nogodoss/article/details/17246489 最近搞横竖屏,获得一些心得,特记录下来. 做横竖屏最重要的是确定横竖屏响应的接口.目 ...
- CSS-div漂浮
显示效果如下: 代码如下: <div style="width:100%;border-top:0px solid #999999;"> <div style=& ...
- hdu1430魔板(BFS+康托展开)
做这题先看:http://blog.csdn.net/u010372095/article/details/9904497 Problem Description 在魔方风靡全球之后不久,Rubik先 ...
- 为 vsftpd 启动 vsftpd:500 OOPS: bad bool value in config file for: pasv_enable
每行的值都不要有空格,否则启动时会出现错误,举个例子,假如我在listen=YES后多了个空格,那我启动时就出现.. 为 vsftpd 启动 vsftpd:500 OOPS: bad bool val ...
- [React] React Fundamentals: Mixins
Mixins will allow you to apply behaviors to multiple React components. Components are the best way t ...
- CSS3画腾讯QQ图标 无图片和js参考
css代码: body { font: 12px Tahoma,arial,sans-serif; margin:; padding:; } #mask { left:; opacity: 0.2; ...
- Java 向Hbase表插入数据报(org.apache.hadoop.hbase.client.HTablePool$PooledHTable cannot be cast to org.apac)
org.apache.hadoop.hbase.client.HTablePool$PooledHTable cannot be cast to org.apac 代码: //1.create HTa ...
- linux调度器 信息解读
http://blog.csdn.net/wudongxu/article/category/791519