[LeetCode]Count Primes
2016-03-26 10:51
274 查看
Description:
Count the number of prime numbers less than a non-negative number, n.
参考https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes
Count the number of prime numbers less than a non-negative number, n.
参考https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes
class Solution { public: int countPrimes(int n) { if(n<=2) return 0; vector<bool> isprime(n,true); for(int i=3; i*i<n; i=i+2){ if(isprime[i]){ for(int j=i*i; j<n; j+=2*i){ //隔开n个乘积,跳过偶数还有优化 isprime[j] = false; } } } int count = 1; for(int i=3; i<n; i+=2){ if(isprime[i]) count++; } return count; } };
相关文章推荐
- Cocos2d-x笔记记忆整理Day2
- 实验二 单元测试
- 和我一起学批处理(2)
- Html5 CSS3新标签解释及用法
- 知识点
- hbase错误记录一; File /hbase/.tmp/hbase.version could only be replicated to 0 nodes instead of minReplica
- ubuntu 安装nodejs(很好用)
- Unity3D 学习笔记5 ——使用ScriptableObject进行序列化
- 日志管理方法和装置
- 你只是追逐时髦的码农
- Android开发有用的站点
- [JS][jQuery]remove()与 empty()的差别
- python 下的数据结构与算法---5:递归(Recursion)
- 互联网公司IT系统架构进化之路
- H264 MVD&MVP
- Android Event Listen
- 二叉树、树、森林的相互转换
- [LeetCode][二叉树]Balanced Binary Tree
- PHP中删除数组中的元素详解(unset,array_splice)数组元素增删汇总
- 中国和中国的邻居