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的更多相关文章

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

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

  2. [LeetCode] 204. Count Primes 质数的个数

    Count the number of prime numbers less than a non-negative number, n. Example: Input: 10 Output: 4 E ...

  3. [LeetCode] 204. Count Primes 计数质数

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

  4. Java [Leetcode 204]Count Primes

    题目描述: Description: Count the number of prime numbers less than a non-negative number, n. 解题思路: Let's ...

  5. LeetCode 204. Count Primes (质数的个数)

    Description: Count the number of prime numbers less than a non-negative number, n. 题目标签:Hash Table 题 ...

  6. Java for LeetCode 204 Count Primes

    Description: Count the number of prime numbers less than a non-negative number, n. 解题思路: 空间换时间,开一个空间 ...

  7. (easy)LeetCode 204.Count Primes

    Description: Count the number of prime numbers less than a non-negative number, n. Credits:Special t ...

  8. [LeetCode] 204. Count Primes 解题思路

    Count the number of prime numbers less than a non-negative number, n. 问题:找出所有小于 n 的素数. 题目很简洁,但是算法实现的 ...

  9. 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的 ...

随机推荐

  1. 为什么要用 WebSocket

    使用传统的 HTTP 轮询或者长连接的方式也可以实现类似服务器推送的效果,但是这类方式都存在资源消耗过大或推送延迟等问题.而 WebSocket 直接使用 TCP 连接保持全双工的传输,可以有效地减少 ...

  2. coocs2d-x 分辨率

    config.lua: CC_DESIGN_RESOLUTION = { width = , height = , autoscale = "FIXED_HEIGHT", call ...

  3. 【转】OpenGL超级宝典笔记——纹理映射Mipmap

    原文地址 http://my.oschina.net/sweetdark/blog/177812 , 感谢作者,若非法转载请联系本人. 目录[-] Mipmapping Mipmap过滤 构建Mip层 ...

  4. HTTP Request Method共计15种

    更多信息可参考http://tools.jb51.net/table/http_request_method

  5. Linux sudo 命令的应用

    .note-content { font-family: "Helvetica Neue", Arial, "Hiragino Sans GB", STHeit ...

  6. oracle--第一天议--bai

    第一天: 1 oracle的安装 a 卸载 b 安装服务器软件及数据库(orcl) --OracleServiceOrcl c 执行网络配置--配置监听1521,本地net服务名(创建1个外部连接的u ...

  7. R for循环之break,next

    next跳出本次循环 break跳出本层循环(当有多个for 循环时,即跳出最近的一个for循环)

  8. java 学习框架

    例如 Jsp.Velocity.Tiles.iText 和 POI.Spring MVC框架并不知道使用的视图,所以不会强迫您只使用 JSP 技术.

  9. C# GetType与typeof

    在反射和泛型中经常会使用到Type类,获取Type的最常用的方法是 obj.GetType(),和typeof(T).在获取泛型的type时有些小坑. public static void Main( ...

  10. 使用vlc进行二次开发做自己的播放器

    可参考: 使用vlc播放器做rtsp服务器 使用vlc播放器播放rtsp视频 web网页中使用vlc插件播放相机rtsp流视频 使用 https://github.com/ZeBobo5/Vlc.Do ...