Python3解leetcode 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.
思路:
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的更多相关文章
- Python3解leetcode Count Binary Substrings
问题描述: Give a string s, count the number of non-empty (contiguous) substrings that have the same numb ...
- [leetcode] Count Primes
Count Primes Description: Count the number of prime numbers less than a non-negative number, n click ...
- [LeetCode] Count Primes 质数的个数
Description: Count the number of prime numbers less than a non-negative number, n click to show more ...
- 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 ...
- Python3解leetcode Number of Boomerangs
问题描述: Given n points in the plane that are all pairwise distinct, a "boomerang" is a tuple ...
- 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 ...
- Python3解leetcode Rotate Array
问题描述: Given an array, rotate the array to the right by k steps, where k is non-negative. Example 1: ...
- LeetCode Count Primes 求素数个数(埃拉托色尼筛选法)
题意:给一个数n,返回小于n的素数个数. 思路:设数字 k =from 2 to sqrt(n),那么对于每个k,从k2开始,在[2,n)范围内只要是k的倍数的都删掉(也就是说[k,k2)是不用理的, ...
- 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 ...
随机推荐
- windows 命令端口映射
显示映射列表netsh interface portproxy show all 将本地的1234端口的数据转发至192.168.x.x上的1234端口netsh interface portprox ...
- -bash: ./hello.jar: 无法执行二进制文件
在linux中直接调用java包产生的 解决:依赖多个包要用冒号分隔,而不是分号 正确:> java -cp ./lib/*:./hello.jar hello 错误:> java -cp ...
- linux点滴记录
以下均为在Ubuntu下实践操作 更改DNS //编辑文件 - “/etc/resolv.conf”,打开“终端应用程序”-“附件” - “终端”,在终端里输入下面的命令: sudo nano /et ...
- 进程池Pool的简单使用,同步异步的区别
#进程池 """ 当需要创建子进程数量不多的时候,可以直接利用multiprocessing 中的Process动态生成多个进程,但是如果上百甚至上千个任务, " ...
- java.lang.IllegalStateException: Cannot forward after response has been committed
jjava.lang.IllegalStateException: Cannot forward after response has been committed at org.apache.cat ...
- zookeeper+dubbo+demo
zookeeper下载地址 https://archive.apache.org/dist/zookeeper/ zookeeper安装和使用 windows环境 https://blog.csdn. ...
- 洛谷 P1168 中位数(优先队列)
题目链接 https://www.luogu.org/problemnew/show/P1168 解题思路 这个题就是求中位数,但是暴力会tle,所以我们用一种O(nlogn)的算法来实现. 这里用到 ...
- SELECT COUNT语句
数据库查询相信很多人都不陌生,所有经常有人调侃程序员就是CRUD专员,这所谓的CRUD指的就是数据库的增删改查. 在数据库的增删改查操作中,使用最频繁的就是查询操作.而在所有查询操作中,统计数量操作更 ...
- 38.0到n-1中缺失的数字
题目描述: 一个长度为n-1的递增排序数组中的所有数字都是唯一的,并且每个数字的范围都在0到n-1之内.在范围0到n-1内的n个数字中有且仅有一个数字不在该数组中,求出该数字. 思路分析: 因 ...
- python常用函数 P
popleft(iterable) 对应pop,左侧弹出,队列适用. 例子: permutations(iterable, int) itertools的permutations方法可以产生集合的所有 ...