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的更多相关文章

  1. 【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 ...

  2. &lt;LeetCode OJ&gt; 204. Count Primes

    Description: Count the number of prime numbers less than a non-negative number, n. 分析: 思路首先:一个数不是合数就 ...

  3. [leetcode] 204. Count Primes 统计小于非负整数n的素数的个数

    题目大意 https://leetcode.com/problems/count-primes/description/ 204. Count Primes Count the number of p ...

  4. 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 ...

  5. 204. Count Primes - LeetCode

    Queston 204. Count Primes Solution 题目大意:给一个数,求小于这个数的素数的个数 思路:初始化一个boolean数组,初始设置为true,先遍历将2的倍数设置为fal ...

  6. 【LeetCode】 204. Count Primes 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 素数筛法 参考资料 日期 [LeetCode] 题目 ...

  7. 【刷题-LeetCode】204. Count Primes

    Count Primes Count the number of prime numbers less than a non-negative number, *n*. Example: Input: ...

  8. (easy)LeetCode 204.Count Primes

    Description: Count the number of prime numbers less than a non-negative number, n. Credits:Special t ...

  9. [LeetCode] 204. Count Primes 质数的个数

    Count the number of prime numbers less than a non-negative number, n. Example: Input: 10 Output: 4 E ...

  10. 【LeetCode】204 - Count Primes

    Description:Count the number of prime numbers less than a non-negative number, n. Hint: Let's start ...

随机推荐

  1. pycharm terminal打开在虚拟环境

    pycharm调试是虚拟环境,terminal不是虚拟环境,搞了好久,原来需要激活一下 cd   venv\Scripts> 去虚拟环境的Script目录下,运行activate.bat激活环境 ...

  2. Seeker:一款可获取高精度地理和设备信息的工具分析

    Seeker是一款可以获取高精度地理和设备信息的工具.其利用HTML5,Javascript,JQuery和PHP来抓取设备信息,以及Geolocation接口实现对设备高精度地理位置的获取. See ...

  3. 日志:slf4j+logback 的配置与使用

    1. 常用日志组件和选择 java开发日志处理是发现和调试bug所 必不可少的,那么现在企业中常用的日志组件有哪些呢,JCL . JUL.  SLF4j.Log4j.  Log4j2 . Logbac ...

  4. nested exception is org.apache.ibatis.reflection.ReflectionException: There is no getter for property named 'enterpriseId' in 'class java.lang.String'

    错误信息: nested exception is org.apache.ibatis.reflection.ReflectionException: There is no getter for p ...

  5. 使用GDB和GEF进行调试

    使用GDB进行调试 这是编译ARM二进制文件和使用GDB进行基本调试的简单介绍.在您按照教程进行操作时,您可能需要按照自己的习惯使用ARM程序集.在这种情况下,你要么需要一个备用的ARM设备,或者你只 ...

  6. 关于python导包问题

    讨论采用 * 模糊导入或者 单独导入变量 会在不同文件生成不同的对象 .a └── mypackage ├── a.py ├── b.py ├── c.py   b.py内容如下 import c d ...

  7. Linux SWAP交换分区维护

    1.查看当前swap分区信息

  8. particlesjs

    今天发现一个粒子动画的插件下个笔记做个备用: <!DOCTYPE html> <html lang="en"> <head> <meta ...

  9. Codeforces 991 车牌号组合数学计算

    A /*Huyyt*/ #include<bits/stdc++.h> #define mem(a,b) memset(a,b,sizeof(a)) #define pb push_bac ...

  10. 防sql注入方法

    mysql_escape_string(strip_tags($arr)) /** * 函数名称:post_check() * 函数作用:对提交的编辑内容进行处理 * 参 数:$post: 要提交的内 ...