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 ...
随机推荐
- python之jupyter安装与使用
Jupyter Notebook 的本质是一个 Web 应用程序,便于创建和共享文学化程序文档,支持实时代码,数学方程,可视化和 markdown.用途包括:数据清理和转换,数值模拟,统计建模,机器学 ...
- js单选和全选
列子: //拿到选中的单选框 一.var objs = $(".detail") for(var obj in objs){ var isCheck = obj.is(':chec ...
- java复制文件范例代码
String url1 = "F:\\SuperMap-Projects\\region.udb";// 源文件路径 try { for(int i=1;i<101;i++) ...
- idea安装完成后要做的几件事(设置字体、编码、行号)
1.设置字体大小和样式 打开设置:File-->Settings 看到如下界面,输入font,点击Editor目录下的Font设置字体大小和样式: Font:字体样式 size:字体大小 Fal ...
- java多线程ExecutorService
1.new Thread的弊端 执行一个异步任务你还只是如下new Thread吗? new Thread(new Runnable() { @Override public void run() { ...
- 如何使用sqlalchemy获取表的主键、以及每一个字段名和对应类型
使用sqlalchemy获取到的结果只包含数据,不包含字段,那么我们如何获取到对应字段和其属性呢?以及如何获取某张表的主键呢? # -*- coding:utf-8 -*- # @Author: Wa ...
- pyquery:轻松、灵活的处理html
介绍 pyquery是一个专门用来解析html的库,从名字很容易想到jQuery,没错,这完全是仿照jQuery的语法实现的.如果用过jQuery,那么pyquery也很容易上手 初始化html py ...
- centos7.2升级openssh7.9p1
Centos7.2版本yum升级openssh版本最高到7.4,想要升级到更高的版本需要重新编译 一.查看当前openssh版本: [root@localhost ~]# ssh -VOpenSSH_ ...
- HQL实现模糊查询
hibernate 实现模糊查询两种传参方式,其实各个方法的实质都是一样的,只不过传递参数的方法稍微有点区别 public List<User> getUsers(String id){ ...
- windows下双击可运行的Java软件打包方案(转)
出处: http://www.cnblogs.com/shiyangxt/ 刚开始学Java的时候,挺郁闷的,写出来的java类文件,需要dos下编译,然后再dos下运行看效果.这使初学者常常 觉得麻 ...