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 ...
随机推荐
- 设置Linux之CentOS7的网络的两种方式动态IP+静态IP
1 动态IP 参考之前的文章 点击进入 2 静态IP vi /etc/sysconfig/network-scripts/ifcfg-ens33 详情配置如下,上面半部分是我之前的动态IP的设置 静态 ...
- String,到底创建了多少个对象?
String str=new String("aaa"); <span style="font-size:14px;">String str=n ...
- magento获取当前栏目ID号与栏目名称函数
Magento获取当前栏目ID:$_cat= new Mage_Catalog_Block_Navigation();$curent_cat= $_cat->getCurrentCategory ...
- hive建表结构
drop table dw.fct_so;create table dw.fct_so(so_id bigint comment '订单ID',parent_so_id bigint comment ...
- shoeBox超实用的雪碧图(Sprite)图制作工具-使用
从前端优化说起 浏览器载入单张图片的速度基本取决于图片的大小,但是载入多张图片的速度却和另一个要素息息相关-网络请求数,在图片大小和一致的情况下,图片张数越少其请求数越少其载入速度也就越快.所以在使用 ...
- mysql innodb存储引擎 锁 事务
以下内容翻译自mysql5.6官方手册. InnoDB是一种通用存储引擎,可平衡高可靠性和高性能.在MySQL 5.6中,InnoDB是默认的MySQL存储引擎.除非已经配置了不同的默认存储引擎, ...
- springboot异常
异常如下: org.springframework.context.ApplicationContextException: Unable to start embedded container; n ...
- 磁盘IO及性能指标
一.磁盘 I/O 的概念 I/O 的概念,从字义来理解就是输入输出.操作系统从上层到底层,各个层次之间均存在 I/O.比如,CPU 有 I/O,内存有 I/O, VMM 有 I/O, 底层磁盘上也有 ...
- linux学习:【第3篇】之常见命令2
一.知识点回顾 临时:关闭当前正在运行的 /etc/init.d/iptables stop 永久:关闭开机自启动 chkonfig iptables off ll /var/log/secure # ...
- tp5.1引用第三方类库
1.TP5第三方类库全部放在Extend目录内,如果是放在Extend目录下,则不需要再类库里声明namespace.直接 new \YourClass() 即可 2.如果你的类库在Extend的子目 ...