[LeetCode] 204. 计数质数

题目链接:https://leetcode-cn.com/problems/count-primes/

题目描述:

统计所有小于非负整数 n 的质数的数量。

示例:

输入: 10
输出: 4
解释: 小于 10 的质数一共有 4 个, 它们是 2, 3, 5, 7 。

思路:

质数就是除了 1 和本身找不到其他能除尽的数,思路请看题目的提示!

思路一:暴力法(超时)(大家可以学习一下 for ... else 的用法, 一般配合 break 使用)

class Solution:
    def countPrimes(self, n: int) -> int:
        res = 0
        for i in range(2, n):
            for j in range(2, i):
                if i % j == 0:
                    break
            else:
                #print(i)
                res += 1
        return res

思路二:优化暴力(超时),我们验证质数可以不需要小于它的数都验证

class Solution:
    def countPrimes(self, n: int) -> int:
        res = 0
        for i in range(2, n):
            for j in range(2, int(i ** 0.5) + 1):
                if i % j == 0:
                    break
            else:
                # print(i)
                res += 1
        return res

思路三:厄拉多塞筛法(好像能过)

class Solution:
    def countPrimes(self, n: int) -> int:
        isPrimes = [1] * n
        res = 0
        for i in range(2, n):
            if isPrimes[i] == 1: res += 1
            j = i
            while i * j < n:
                isPrimes[i * j] = 0
                j += 1
        return res

思路四:综上一起优化(超过90%)

class Solution:
    def countPrimes(self, n: int) -> int:
        if n < 2: return 0
        isPrimes = [1] * n
        isPrimes[0] = isPrimes[1] = 0
        for i in range(2, int(n ** 0.5) + 1):
            if isPrimes[i] == 1:
                isPrimes[i * i: n: i] = [0] * len(isPrimes[i * i: n: i])
        return sum(isPrimes)

原文地址:https://www.cnblogs.com/powercai/p/11370297.html

时间: 2024-08-12 16:08:53

[LeetCode] 204. 计数质数的相关文章

204. 计数质数 | Count Primes

Count the number of prime numbers less than a non-negative number, n. Example: Input: 10 Output: 4 Explanation: There are 4 prime numbers less than 10, they are 2, 3, 5, 7. 统计所有小于非负整数 n 的质数的数量. 示例: 输入: 10 输出: 4 解释: 小于 10 的质数一共有 4 个, 它们是 2, 3, 5, 7 .

204计数质数

题目:统计所有小于非负整数 n 的质数的数量. 来源:https://leetcode-cn.com/problems/count-primes/ 法一:自己的超时代码 思路:和官方的方法事实上一样,但是代码没有用标记0 1的方法,导致很费时.删除每个质数的倍数时,都需要判断是否存在,如果是用标记0 1的方法不会出现这种问题,要学会这个切片技巧. import math class Solution: def countPrimes(self, n: int) -> int: if n in [

204.计数质数

感觉挺简单的 结果掉坑了 超时警告 class Solution { public int countPrimes(int n) { int temp = 0; for(int i = 0 ;i < n ;i++){ if(isPrimeNumber(i)){ temp++; } } return temp; } public boolean isPrimeNumber(int n) { if(n == 1||n == 0) return false; if(n==2||n==3) { retu

leetcode 204/187/205 Count Primes/Repeated DNA Sequences/Isomorphic Strings

一:leetcode 204 Count Primes 题目: Description: Count the number of prime numbers less than a non-negative number, n 分析:此题的算法源码可以参看这里,http://en.wikipedia.org/wiki/Sieve_of_Eratosthenes 代码: class Solution { public: int countPrimes(int n) { // 求小于一个数n的素数个

LeetCode 204 Count Primes(质数计数)(*)

翻译 计算小于一个非负整数n的质数的个数. 原文 Count the number of prime numbers less than a non-negative number, n. 分析 这道题以前遇到过,当时是用的最笨的办法,现在也没什么好想法,又恰好题目有提示,我就点开了.题目的提示是一条一条给出来的,我也就逐个的全点开了,感觉好失败-- public int countPrimes(int n) { int count = 0; for (int i = 1; i < n; i++

【leetcode 简单】 第五十八题 计数质数

统计所有小于非负整数 n 的质数的数量. 示例: 输入: 10 输出: 4 解释: 小于 10 的质数一共有 4 个, 它们是 2, 3, 5, 7 . class Solution: def countPrimes(self, n): """ :type n: int :rtype: int """ isPrime = [1] * max(2, n) isPrime[0],isPrime[1]=False,False x = 2 while x

LeetCode. 计数质数

题目要求: 统计所有小于非负整数 n 的质数的数量. 示例i: 输入: 10 输出: 4 解释: 小于 10 的质数一共有 4 个, 它们是 2, 3, 5, 7 . 代码: class Solution { public: int countPrimes(int n) { if (n <= 2) return 0; vector<bool> prime(n, true); for(int i = 2; i*i < n; i++) { if(prime[i]) { for(int

leetcode 204

题目描述: Description: Count the number of prime numbers less than a non-negative number, n. 解法一: 遍历从1-n的所有整数,查看是否为质数,是质数借助一个则将该整数存入一个容器中,判断一个数是否为质数,可以遍历在容器中且小于n的平方根的质数,如果n可以被符合条件的质数整除,则这个数不是质数.代码如下: class Solution { public: vector<int> prime_vec; bool

leetcode 204. Count Primes 找出素数的个数 ---------- java

Description: Count the number of prime numbers less than a non-negative number, n. 找出小于n的素数个数. 1.用最淳朴的算法果然超时了. public class Solution { public int countPrimes(int n) { if (n < 2){ return 0; } int result = 0; for (int i = 2; i < n; i++){ if (isPrimes(