leetcode-easy-math-204 Count Primes-NO
mycode time limited
class Solution(object):
def countPrimes(self, n):
"""
:type n: int
:rtype: int
"""
if n < 3: return 0 def is_primes(x):
for i in range(2,x):
if x % i == 0:
return False
return True count = 0 for i in range(2,n):
if is_primes(i) :
print(i)
count += 1
return count
参考
1
class Solution(object):
def countPrimes(self, n):
"""
:type n: int
:rtype: int
"""
if n <= 2:
return 0 prime = [1] * n
prime[0] = prime[1] = 0 for index in range(2, n):
if prime[index] == 1:
time = 2
while index * time < n:
prime[index * time] = 0
time += 1 return sum(prime)
2
class Solution:
def countPrimes(self, n: int) -> int:
if n < 2: return 0 prime = [1]*n for i in range(2,int(n*0.5)+1):
prime[i*i:n:i] = [0] * len(prime[i*i:n:i]) return sum(prime)-2 #-2 because 0 and 1 is not a prime number
更快优化
import math class Solution(object):
def countPrimes(self, n):
"""
:type n: int
:rtype: int
""" if n < 2:
return 0
s = [1] * n
s[0] = s[1] = 0
for i in range(2, int(n ** 0.5) + 1):
if s[i] == 1:
s[i*i:n:i] = [0] * int((n-i*i-1)/i + 1)
return sum(s)
time limited
class Solution(object):
def countPrimes(self, n):
"""
:type n: int
:rtype: int
"""
count = 0
for i in range(2,n):
count += self.isPrime(i)
return count def isPrime(self,x):
x_= int(x**0.5+1)
for i in range(2,x_):
if x % i == 0:
return 0
return 1
leetcode-easy-math-204 Count Primes-NO的更多相关文章
- 【leetcode❤python】 204. Count Primes
#-*- coding: UTF-8 -*- #Hint1:#数字i,i的倍数一定不是质数,因此去掉i的倍数,例如5,5*1,5*2,5*3,5*4,5*5都不是质数,应该去掉#5*1,5*2,5*3 ...
- <LeetCode OJ> 204. Count Primes
Description: Count the number of prime numbers less than a non-negative number, n. 分析: 思路首先:一个数不是合数就 ...
- [leetcode] 204. Count Primes 统计小于非负整数n的素数的个数
题目大意 https://leetcode.com/problems/count-primes/description/ 204. Count Primes Count the number of p ...
- leetcode 263. Ugly Number 、264. Ugly Number II 、313. Super Ugly Number 、204. Count Primes
263. Ugly Number 注意:1.小于等于0都不属于丑数 2.while循环的判断不是num >= 0, 而是能被2 .3.5整除,即能被整除才去除这些数 class Solution ...
- 204. Count Primes - LeetCode
Queston 204. Count Primes Solution 题目大意:给一个数,求小于这个数的素数的个数 思路:初始化一个boolean数组,初始设置为true,先遍历将2的倍数设置为fal ...
- 【LeetCode】 204. Count Primes 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 素数筛法 参考资料 日期 [LeetCode] 题目 ...
- 【刷题-LeetCode】204. Count Primes
Count Primes Count the number of prime numbers less than a non-negative number, *n*. Example: Input: ...
- (easy)LeetCode 204.Count Primes
Description: Count the number of prime numbers less than a non-negative number, n. Credits:Special t ...
- [LeetCode] 204. Count Primes 质数的个数
Count the number of prime numbers less than a non-negative number, n. Example: Input: 10 Output: 4 E ...
- 【LeetCode】204 - Count Primes
Description:Count the number of prime numbers less than a non-negative number, n. Hint: Let's start ...
随机推荐
- Centos7:dubbo监控中心安装,配置和使用
制作dubbo-admin.war文件 下载dubbo-admin https://github.com/alibaba/dubbo 注:2.6版本后源码中不包含dubbo-admin工程 在dubb ...
- oracle数据库连接问题org.springframework.jdbc.support.MetaDataAccessException: JDBC DatabaseMetaData method not implemented by JDBC driver - upgrade your driver...
org.springframework.jdbc.support.MetaDataAccessException: JDBC DatabaseMetaData method not implement ...
- css3中单位rem与.less结合布局
rem是CSS3新增的一个相对单位(root em,根em),这个单位引起了广泛关注.这个单位与em有什么区别呢?区别在于使用rem为元素设定字体大小时,仍然是相对大小,但相对的只是HTML根元素. ...
- Java构造二叉树、树形结构先序遍历、中序遍历、后序遍历
package com.example.demo; public class BTree { public int data; public BTree left; public BTree rigt ...
- 2019.10.9wechat反弹shell复现
./backdoor.py -f libEGL.dll -s reverse_shell_tcp_inline -P 6666 -H 192.168.106.137 msfconsle 打开msf 在 ...
- mybaites 入门到精通
这个mybatis教程也不错:http://limingnihao.iteye.com/blog/781671 MyBatis 目录(?)[-] mybatis实战教程mybatis in actio ...
- CentOS 安装 oralce Java的图形出错: libXtst.so.6: cannot open shared object file: No such file or directory
问题类似: shared object file: No such file or directory occurred..java.lang.UnsatisfiedLinkError: /tmp/O ...
- backtop返回页面顶部jquery代码
<div id="toTop" style="width:30px;height:110px;border:1px solid #74B9DC; border-ra ...
- centos7安装kong和kong-dashboard
1.安装Kong yum install -y https://kong.bintray.com/kong-community-edition-rpm/centos/7/kong-community- ...
- VScode VUE+PYTHON习惯用的编辑器
本人从事运维开发工作,经常要开发一些web系统工具,这就需要用到前后端开发.vscode是我用得最舒服的一种编辑器,前端后端都习惯在上面写,这里记录一些配置. 一,插件安装 目前我最主要用的是: ...