Description:

Count the number of prime numbers less than a non-negative number, n

click to show more hints.

Credits:
Special thanks to @mithmatt for adding this problem and creating all test cases.

计数出小于非负整数n的质数数量。质数(prime number)又称素数,有无限个。质数定义为在大于1的自然数中,除了1和它本身以外不再有其他因数。

解法:埃拉托斯特尼筛法 Sieve of Eratosthenes

如果一个数是另一个数的倍数,那这个数肯定不是质数。利用这个性质,可以建立一个质数数组,从2开始将素数的倍数都标注为不是质数。第一轮将4、6、8等表为非质数,然后遍历到3,发现3没有被标记为非质数,则将6、9、12等标记为非质数,一直到N为止,再数一遍质数数组中有多少质数。

Java:

public class Solution {
public int countPrimes(int n) {
boolean[] prime = new boolean[n];
Arrays.fill(prime, true);
for(int i = 2; i < n; i++){
if(prime[i]){
// 将i的2倍、3倍、4倍...都标记为非素数
for(int j = i * 2; j < n; j = j + i){
prime[j] = false;
}
}
}
int count = 0;
for(int i = 2; i < n; i++){
if(prime[i]) count++;
}
return count;
}
}

Python:

class Solution:
# @param {integer} n
# @return {integer}
def countPrimes(self, n):
isPrime = [True] * max(n, 2)
isPrime[0], isPrime[1] = False, False
x = 2
while x * x < n:
if isPrime[x]:
p = x * x
while p < n:
isPrime[p] = False
p += x
x += 1
return sum(isPrime)

Python:

class Solution(object):
def countPrimes(self, n):
"""
:type n: int
:rtype: int
"""
if n <= 2: return 0
vis = [False] * n
for i in range(2, int(n ** 0.5) + 1):
if vis[i]: continue
j = i
while j * i < n:
vis[j * i] = True
j += 1
ans = 0
for i in range(2, n):
if not vis[i]: ans += 1
return ans

C++:

class Solution {
public:
int countPrimes(int n) {
if(!n||n==1) return 0;
vector<bool> isPrime(n,true);
// Loop's ending condition is i * i < n instead of i < sqrt(n)
// to avoid repeatedly calling an expensive function sqrt().
for(int i=2;i*i<n;++i)
{
if(!isPrime[i]) continue;
//填表起点i*i,如3*3,因为3*2已填,步长+i
for(int j=i*i;j<n;j+=i)
{
isPrime[j]=false;
}
}
int count=0;
for(int i=2;i<n;++i)
{
if(isPrime[i]) ++count;
}
return count;
}
};

  

All LeetCode Questions List 题目汇总

  

[LeetCode] 204. Count Primes 计数质数的更多相关文章

  1. LeetCode 204. Count Primes计数质数 (C++)

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

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

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

  3. 204 Count Primes 计数质数

    计算所有小于非负整数 n 的质数数量. 详见:https://leetcode.com/problems/count-primes/description/ Java实现: 埃拉托斯特尼筛法:从2开始 ...

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

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

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

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

  6. LeetCode 204 Count Primes

    Problem: Count the number of prime numbers less than a non-negative number, n. Summary: 判断小于某非负数n的质数 ...

  7. Java [Leetcode 204]Count Primes

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

  8. Java for LeetCode 204 Count Primes

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

  9. Leetcode 204 Count Primes 数论

    题意:统计小于n的质数个数. 作为一个无节操的楼主,表示用了素数筛法,并没有用线性素数筛法. 是的,素数筛法并不是该题最佳的解法,线性素数筛法才是. 至于什么是素数筛法,请百度吧. class Sol ...

随机推荐

  1. jquery对象转成dom对象

     jQuery库本质上还是JavaScript代码,它只是对JavaScript语言进行包装处理,为的是提供更好更方便快捷的DOM处理与开发中经常使用的功能.我们使用jQuery的同时也能混合Java ...

  2. 拿webshell方法汇总

    地址:https://blog.csdn.net/Breeze_CAT/article/details/80044676

  3. springmvc的控制器是不是单例模式,如果是,有什么问题,怎么解决?

    默认情况下是单例模式, 在多线程进行访问的时候,有线程安全问题. 但是不建议使用同步,因为会影响性能. 解决方案,是在控制器里面不能写成员变量. 为什么设计成单例设计模式? 1.性能(不用每次请求都创 ...

  4. LG4781 【模板】拉格朗日插值 和 JLOI2016 成绩比较

    [模板]拉格朗日插值 题目描述 由小学知识可知,$n$个点$(x_i,y_i)$可以唯一地确定一个多项式 现在,给定$n$个点,请你确定这个多项式,并将$k$代入求值 求出的值对$998244353$ ...

  5. 学习Microsoft Visio(1)

    基础篇 一.认识Visio 1.Visio是什么 Visio最初属于Visio公司,该公司成立于1990年9月.1992年,公司更名为Shapeware.同年11月,它发布了他们公司的第一个产品:Vi ...

  6. py3+requests+json+xlwt,爬取拉勾招聘信息

    在拉勾搜索职位时,通过谷歌F12抓取请求信息 发现请求是一个post请求,参数为: 返回的是json数据 有了上面的基础,我们就可以构造请求了 然后对获取到的响应反序列化,这样就获取到了json格式的 ...

  7. AtCoder Beginner Contest 127 解题报告

    传送门 非常遗憾.当天晚上错过这一场.不过感觉也会掉分的吧.后面两题偏结论题,打了的话应该想不出来. A - Ferris Wheel #include <bits/stdc++.h> u ...

  8. H5中实现加载更多的逻辑及代码执行。

    H5中加载更多的逻辑总结: 1.首先,需要三个底部的提示,分别是“加载中”.“--我是有底线的--”.“暂时没有记录”,当然,这三句话根据不同的项目,可以自定义.具体代码例子如下: <div c ...

  9. 洛谷 题解 UVA572 【油田 Oil Deposits】

    这是我在洛谷上的第一篇题解!!!!!!!! 这个其实很简单的 我是一只卡在了结束条件这里所以一直听取WA声一片,详细解释代码里见 #include<iostream> #include&l ...

  10. [bzoj1008] 越狱

    Description 监狱有连续编号为1...N的N个房间,每个房间关押一个犯人,有M种宗教,每个犯人可能信仰其中一种.如果相邻房间的犯人的宗教相同,就可能发生越狱,求有多少种状态可能发生越狱 数据 ...