LeetCode 204 Count Primes
Problem:
Count the number of prime numbers less than a non-negative number, n.
Summary:
判断小于某非负数n的质数个数。
Solution:
用所谓的“刷质数表”的方式先用HashTable记录小于n的所有数是质数还是合数,再逐一数出。
看了题目中的Hint才知道这种方法有一个更加高大上的名字Sieve of Eratosthenes
即在每判断一个数为质数时,将它的bei'shu倍数均计为合数。
class Solution {
public:
int countPrimes(int n) {
if (n == || n == ) {
return ;
}
vector<int> prime(n, );
prime[] = prime[] = ;
for (long long i = ; i < n; i++) {
if (!prime[i]) {
for (long long j = i * i; j < n; j += i) {
prime[j] = ;
}
}
}
int cnt = ;
for (int i = ; i < n; i++) {
if (!prime[i]) {
cnt++;
}
}
return cnt;
}
};
LeetCode 204 Count Primes的更多相关文章
- [leetcode] 204. Count Primes 统计小于非负整数n的素数的个数
题目大意 https://leetcode.com/problems/count-primes/description/ 204. Count Primes Count the number of p ...
- [LeetCode] 204. Count Primes 质数的个数
Count the number of prime numbers less than a non-negative number, n. Example: Input: 10 Output: 4 E ...
- [LeetCode] 204. Count Primes 计数质数
Description: Count the number of prime numbers less than a non-negative number, n click to show more ...
- Java [Leetcode 204]Count Primes
题目描述: Description: Count the number of prime numbers less than a non-negative number, n. 解题思路: Let's ...
- LeetCode 204. Count Primes (质数的个数)
Description: Count the number of prime numbers less than a non-negative number, n. 题目标签:Hash Table 题 ...
- Java for LeetCode 204 Count Primes
Description: Count the number of prime numbers less than a non-negative number, n. 解题思路: 空间换时间,开一个空间 ...
- (easy)LeetCode 204.Count Primes
Description: Count the number of prime numbers less than a non-negative number, n. Credits:Special t ...
- [LeetCode] 204. Count Primes 解题思路
Count the number of prime numbers less than a non-negative number, n. 问题:找出所有小于 n 的素数. 题目很简洁,但是算法实现的 ...
- LeetCode - 204. Count Primes - 埃拉托斯特尼筛法 95.12% - (C++) - Sieve of Eratosthenes
原题 原题链接 Description: Count the number of prime numbers less than a non-negative number, n. 计算小于非负数n的 ...
随机推荐
- 数据可视化 echarts3
初识 echarts ECharts,一个纯 Javascript 的数据可视化图表库,可以流畅的运行在 PC 和移动设备上,兼容当前绝大部分浏览器(IE8/9/10/11,Chrome,Firefo ...
- bat转exe工具 Bat To Exe Converter v2.4.7 绿色版
一款非常小巧的工具,从它的名称便能知道它的功能:它能将BAT或CMD文件转换成 EXE 文件.使用它,你可以保护由自己开发的软件的软件代码,创建一个漂亮的图标,让软件看起来更专业. 下载地址: htt ...
- 洗牌算法Fisher_Yates原理
1.算法 http://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle 简单的原理如下图所示: 2.原理 总结下,洗牌算法Fisher_Yates ...
- C 语言中 malloc、calloc、realloc 和free 函数的使用方法
C标准函数库中,常见的堆上内存管理函数有malloc(), calloc(), recalloc(), free(). 之所以使用堆,是因为栈只能用来保存临时变量.局部变量和函数参数.在函数返回时,自 ...
- centos 6.4 getmail 收取163 邮件
#CentOS 6.6 64bit 默认yum 源没有getmail rpm包#首先安装EPEL yum 源EPEL(Extra Packages for Enterprise Linux):http ...
- vmware workstation安装 Mosrosoft Runtime DLL安装程序未能完成安装
不要点确定.开始菜单运行输入'%temp%',在弹出的窗体中找到一个文件名中含'{132E3257-14F1-411A-BC6C-0CA32D3A9BC6}~setup'(不一定一样,反正就是第一行的 ...
- 转→js数组遍历 千万不要使用for...in...
看到一篇内容还不错,但是排版实在糟糕, 逼死强迫症患者啊,直接拉下去找原文连接,找到了,但是已经消失了···500错误... 第一次因为实在看不下去一篇博客的排版, 为了排版而转载... 转载地址:h ...
- 【IOS】将字体大小不同的文字底部对齐
从WP转IOS了,还是放不下...... 在项目中,要实现如图多个不同大小的文字 底部对齐的效果 像下面这样: (想要的效果) 以为用三个UIFont不同的UILabel 之后让他们底部对齐 ...
- lua获取时间
更多好的文章就在 blog.haoitsoft.com,请大家多多支持! local getTime = os.date("%c"); 其中的%c可以是以下的一种:(注意大小写) ...
- 编译安装PHP7并安装Redis扩展Swoole扩展
编译安装PHP7并安装Redis扩展Swoole扩展 在编译php7的机器上已经有编译安装过php5.3以上的版本,从而依赖库都有了 本php7是编译成fpm-php 使用的, 如果是apache那么 ...