问题描述:

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.

思路:

1、最暴力的方法就是循环遍历,用两个for循环嵌套实现,但是整个代码运行时间太长,提交通不过

2、采用'Sieve of Eratosthenes'方法,该方法的思路是:

①质数是除了1和本身外,不被任何数整除,那么任何一个质数的倍数都不会是质数

②我们知道最小的质数为2,因此从2开始遍历到n-1。

③2的所有倍数都不是质数,所以将2的所有倍数都标记为非质数。

④依次遍历下一个元素,下一个标记为质数的元素一定是质数。因为如果该元素是非质数的话,一定能被除了1和本身外的某一个数x整除,即该数是x的整数倍,而x一定是我们曾经遍历过的数;而依据第三步,我们所有遍历过的数的倍数都被标记为非质数了,我们不可能遍历到非质数,因而相互矛盾;综上即该元素一定是质数

⑤遍历完成后,能够标记所有的数字是否是质数

代码:

class Solution:
def countPrimes(self, n: int) -> int:
count,flag = 0,[True]*(n) #1代表质数,0代表非质数
for i in range(2,n):#从2开始遍历,i代表当前第一个数,i代表这个数所在位置
if flag[i]:#如果当前位置判定为True的话
count += 1
for j in range(i*i,n,i):#将该质数的所有倍数都设定为False,即非质数
flag[j] = False
return count

在循环中尽量不要增加计算,哪怕是加减计算,任何计算量的增加都会增加运行时间,导致提交结果运行时间非常长,结果很差;因而牺牲一点空间,换取时间的大幅缩短也是非常值得的

Python3解leetcode Count Primes的更多相关文章

  1. Python3解leetcode Count Binary Substrings

    问题描述: Give a string s, count the number of non-empty (contiguous) substrings that have the same numb ...

  2. [leetcode] Count Primes

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

  3. [LeetCode] Count Primes 质数的个数

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

  4. Python3解leetcode Maximum SubarrayClimbing Stairs

    问题: You are climbing a stair case. It takes n steps to reach to the top. Each time you can either cl ...

  5. Python3解leetcode Number of Boomerangs

    问题描述: Given n points in the plane that are all pairwise distinct, a "boomerang" is a tuple ...

  6. Python3解leetcode N-ary Tree Level Order Traversal

    问题描述: Given an n-ary tree, return the level order traversal of its nodes' values. (ie, from left to ...

  7. Python3解leetcode Rotate Array

    问题描述: Given an array, rotate the array to the right by k steps, where k is non-negative. Example 1: ...

  8. LeetCode Count Primes 求素数个数(埃拉托色尼筛选法)

    题意:给一个数n,返回小于n的素数个数. 思路:设数字 k =from 2 to sqrt(n),那么对于每个k,从k2开始,在[2,n)范围内只要是k的倍数的都删掉(也就是说[k,k2)是不用理的, ...

  9. Python3解leetcode Linked List Cycle

    问题描述: Given a linked list, determine if it has a cycle in it. To represent a cycle in the given link ...

随机推荐

  1. ubuntu 16.4下hadoop配置伪分布式时出现的坑

    在ubuntu16.4下spark的单机/伪分布式配置我在此就不在一一赘述,详情请点击如下连接: Hadoop安装教程_单机/伪分布式配置_Hadoop2.6.0/Ubuntu14.04 我出现问题是 ...

  2. 【OPCAutomation】 使用OPCAutomation实现对OPC数据的访问

    折腾了一段时间研究OPC,理清了下位机.OPCServer 和OPCClient的关系和通信模型,终于能够来写一篇相关的博客了. 我们使用西门子的 S7 200 SMART作为下位机端,通过3G路由器 ...

  3. HttpSessionBindingListener和HttpSessionAttributeListener区别

    HttpSessionBindingListener和HttpSessionAttributeListener是两个经常让初学者弄混的监听器,其实它们有很大的区别.这2个监听器在文章中简称为Bindi ...

  4. OAuth 2.0 综述

    OAuth 2.0 rfc6749 规范 OAuth 2.0 rfc6749 规范-带目录,阅读 RFC 文档的 工具 OAuth 官网 OAuth2 核心 角色 Token 类型 access to ...

  5. 从0 开始手写一个 RPC 框架,轻松搞定!

    Java技术栈 www.javastack.cn 优秀的Java技术公众号 来源:juejin.im/post/5c4481a4f265da613438aec3 之前在 RPC框架底层到底什么原理得知 ...

  6. Manacher(最长镜面回文串)

    I - O'My! Gym - 101350I Note: this is a harder version of Mirrored string I. The gorillas have recen ...

  7. CodeChef 3-Palindromes(Manacher+dp)

    3-Palindromes   Problem code: PALIN3   Submit All Submissions   All submissions for this problem are ...

  8. 33.Jump Game(跳步游戏)

    Level:   Medium 题目描述: Given an array of non-negative integers, you are initially positioned at the f ...

  9. 攻防世界--Shuffle

    测试文件:https://adworld.xctf.org.cn/media/task/attachments/a03353e605bc436798a7cabfb11be073 1.准备 获得信息 3 ...

  10. C#使用Process启动exe程序,不弹出控制台窗口的方法

    背景:使用wkhtmltopdf工具将html转换成pdf时,这个工具在进行转换时会弹出命令行窗口显示转换过程,但是在项目运行时弹出服务器突然弹出控制台窗口会很奇怪,尤其是当转换多个时.解决这个问题 ...