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. svn服务器地址变换以后,mac下的处理方法

    svn服务器地址变换之后,mac下的处理方法 svn服务器地址变换之后,mac下的处理方法 1.进入终端,进入项目所在的文件夹下: cd 项目位置/ 2.查看svn信息 svn info 3.输出结果 ...

  2. 【BZOJ-3673&3674】可持久化并查集 可持久化线段树 + 并查集

    3673: 可持久化并查集 by zky Time Limit: 5 Sec  Memory Limit: 128 MBSubmit: 1878  Solved: 846[Submit][Status ...

  3. xshell有大量打印时,显示信息不全

    使用xshell远程登录ssh时,编译大型工程或在minicom打印嵌入式设备的信息,发现显示不全. 在网上搜索了一下也没有发现有解决办法. 经过实验发现 xshell terminal type设置 ...

  4. theano broadcasting

    当我们使用函数对两个数组进行计算时,函数会对这两个数组的对应元素进行计算,因此它要求这两个数组有相同的大小(shape相同).如果两个数组的shape不同的话,会进行如下的广播(broadcastin ...

  5. javaScript timer控制

    <script type="text/javascript"> ; //间隔一秒循环执行 var id = setInterval(function () { num ...

  6. JMeter 问题

    1.  JMeter 测试计划 测试计划 使用 JMeter 进行测试的起点,是其它 JMeter 测试元件的容器. 线程组 代表一定数量的并发用户,它可以用来模拟并发用户发送请求.实际的请求内容在S ...

  7. gdb调试器的使用

    想要使用gdb调试程序的话,首先需要gcc -g main.c -o test 然后运行gdb test对程序进行调试 l (小写的l,是list的首字母),用以列出程序 回车    是运行上一个命令 ...

  8. eclipse 快捷键大全(转载)

    Ctrl+1 快速修复(最经典的快捷键,就不用多说了)Ctrl+D: 删除当前行 Ctrl+Alt+↓ 复制当前行到下一行(复制增加)Ctrl+Alt+↑ 复制当前行到上一行(复制增加)Alt+↓ 当 ...

  9. Java网络编程--简单聊天程序

    背景 毕业设计前的练手,学校小比赛中的一个题目. 开发环境 Java(eclipse)+Mysql 简介 使用Java+Mysql开发以个简单的聊天工具,在本次项目中实现了: 1. 用户登录(客户端至 ...

  10. 一个php 字符串判断问题

    先看代码 你觉得下面的代码会输出什么结果: <?php $a = "10"; $b = "1e1"; if($a == $b ) { echo " ...