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 ...
随机推荐
- MySQL体系结构概览
MySQL体系结构 InnoDB体系结构 MySQL实例有一组后台线程.一些内存块和若干服务线程组成 在默认情况下,MySQL有7组后台线程,分别为1个主线程,4组IO线程,1个锁线程,1个错误监控线 ...
- mysql.connector.errors.ProgrammingError: 1698 (28000): Access denied for user 'root'@'localhost'
排错,首先在sql编辑工具 通过测试连接,查看一下自己的密码是否正确 : 由此可见,是自己的密码错误,输入正确的密码后 在这里把密码修改为正确之后程序运行正常 #初始化数据库连接 engine = c ...
- Flask-SQLALchemy动态的filter_by和filter
1.filter_by filter_by用于查询简单的列名,不支持比较运算符. filters = {'name': 'fengyao', 'age': 26} User.query.filter_ ...
- 测开之路六十五:UI测试平台之js
//添加网址的函数,生成一个输入网址的标签,并且把标签append到id为cases下function browser() { var html = '\ <div class="ro ...
- Vagrant 手册之 Multi-machine 多机器
原文地址 Vagrant 可以在一个 Vagrantfile 中定义并控制多个虚拟机.这就是"multi-machine"环境. 这些机器可以协同工作或互相关联.multi-mac ...
- postman的下载和使用
postman的下载 官网:https://www.getpostman.com/downloads/ 创建账号或者用谷歌浏览器账号登录 登录之后,进行接口测试,这里请求百度为例,然后点击send,就 ...
- hihoCoder 1014 : Trie树(字典树)
传送门 Description 小Hi和小Ho是一对好朋友,出生在信息化社会的他们对编程产生了莫大的兴趣,他们约定好互相帮助,在编程的学习道路上一同前进. 这一天,他们遇到了一本词典,于是小Hi就向小 ...
- mooc-IDEA 应用快捷键自动创建测试类--010
十六.IntelliJ IDEA -应用快捷键自动创建测试类 Step1:在类或接口上,按ctrl+shift+t 选择Create New Test... 则在相应测试包下.创建该测试类. 测试类:
- mysql 主从复制(mysql双机热备的实现)
转:http://blog.csdn.net/qq394829044/article/details/53203645 Mysql数据库没有增量备份的机制,当数据量太大的时候备份是一个很大的问题.还好 ...
- 克隆虚拟机(centos7)
当我们做分布式测试时,需要多个节点(虚拟机),除了一个个虚拟机重新安装外,还可以从一个虚拟机镜像克隆出新的虚拟机 本例中要从名为master1的虚拟机克隆一个名为node1的 输入新的虚拟机名称和文件 ...