问题描述:

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. I/O等待事件-db file scattered read

    摘自:http://blog.csdn.net/zq9017197/article/details/7925338

  2. 在Python中操作文件之truncate()方法的使用教程

    在Python中操作文件之truncate()方法的使用教程 这篇文章主要介绍了在Python中操作文件之truncate()方法的使用教程,是Python入门学习中的基础知识,需要的朋友可以参考下 ...

  3. C语言readdir()函数:读取目录函数

    相关函数:open, opendir, closedir, rewinddir, seekdir, telldir, scandir 头文件:#include <sys/types.h> ...

  4. Git001--简介

    Git--简介 本文来自于:https://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000/00 ...

  5. 关于狗书《Flask web开发 基于python的Web开发应用实战》中加入用户隐私功能

    目前是第二次撸狗书,在用户页面这一块我个人觉得有些问题(基于交互设计).按理来说,我作为一个权限只有User的个人用户来说,肯定不喜欢让别人看到我的真实姓名,地址之类的敏感信息.所以我应该是可以设置成 ...

  6. EasyUI的datagrid有值但是显示不出来

    $("#goodsList").datagrid({  url: "../Ajax/GoodsAjax.ashx",  queryParams:  {  cmd ...

  7. JS验证数字

    //1.验证数字 var reg = new RegExp("^[0-9]*$"); //var reg = /^[+]{0,1}(\d+)$|^[+]{0,1}(\d+\.\d+ ...

  8. Java数组相关算法一

    一.数组反转 1.方法一:创建新数组 int[] arr = {6,29,0,4,3}; int[] arr2 = new int[arr.length]; for (int i = 0; i < ...

  9. LeetCode #1021. Remove Outermost Parentheses 删除最外层的括号

    https://leetcode-cn.com/problems/remove-outermost-parentheses/ Java Solution class Solution { public ...

  10. php上传文件如何保证上传文件不被改变或者乱码

    很多网站上传文件都截取文件后缀,前面用时间错加后缀组成,然而一下下载的网站并不需要这样,需要保持原来的文件名,这里讲述一下 //上传操作 function uploadify(){ //var_dum ...