题目大家都非常熟悉,求小于n的所有素数的个数。

自己写的python 代码老是通不过时间门槛,无奈去看了看大家写的code。下面是我看到的投票最高的code:

class Solution:
# @param {integer} n
# @return {integer}
def countPrimes(self, n):
if n < 3:
return 0
primes = [True] * n
primes[0] = primes[1] = False
for i in range(2, int(n ** 0.5) + 1):
if primes[i]:
primes[i * i: n: i] = [False] * len(primes[i * i: n: i])
return sum(primes)

下面的code是有人针对上面这个code进行改进的code:

class Solution(object):
def countPrimes(self, n):
"""
:type n: int
:rtype: int
"""
if n <= 2:
return 0 prime = [True] * n
prime[:2] = [False, False]
for base in xrange(2, int((n - 1) ** 0.5) + 1):
if prime[base]:
prime[base ** 2::base] = [False] * len(prime[base ** 2::base])
return sum(prime)

算法都是一样的,细节处有些不同,比方在对开头if的判断;对prime数组赋值的语句。这里我很好奇,这些改变到底有没有性能的提升,提升了多少。

下面是我的在python下得分析:

➜  ~  python -m timeit -s 'int(3 ** 0.5) + 1'
100000000 loops, best of 3: 0.0119 usec per loop
➜ ~ python -m timeit -s 'int(3 ** 0.5 + 1)' # better but not obiviosly
100000000 loops, best of 3: 0.0117 usec per loop
➜ ~ python -m timeit -s 'l = [False] * 1000' 'l[0] = l[1] = True' # better
10000000 loops, best of 3: 0.102 usec per loop
➜ ~ python -m timeit -s 'l = [False] * 1000' 'l[:2] = [True, True]'
10000000 loops, best of 3: 0.172 usec per loop ➜ ~ python -m timeit -s 'for i in range(100): i >= 2'
100000000 loops, best of 3: 0.0118 usec per loop
➜ ~ python -m timeit -s 'for i in range(100): i > 3' # better but not obiviosly
100000000 loops, best of 3: 0.0116 usec per loop

所以总结下来:

1. 如果我们使用int()之后还要做算术运算,最好先把最终结果算出来,再进行int操作;

2. 如果我们想要对某个数列进行赋值,单个的进行赋值比使用[:]批量赋值要快(这个从实用性来看具体场合具体分析)

3. 如果我们进行大小判断,单纯的><比 >= <=要计算的更快一些。

leetcode-Count Primes 以及python的小特性的更多相关文章

  1. [leetcode] Count Primes

    Count Primes Description: Count the number of prime numbers less than a non-negative number, n click ...

  2. [LeetCode] Count Primes 质数的个数

    Description: Count the number of prime numbers less than a non-negative number, n click to show more ...

  3. leetcode Count and Say python

    class Solution(object): def countAndSay(self, n): """ :type n: int :rtype: str " ...

  4. Python3解leetcode Count Primes

    问题描述: Count the number of prime numbers less than a non-negative number, n. Example: Input: 10 Outpu ...

  5. LeetCode Count Primes 求素数个数(埃拉托色尼筛选法)

    题意:给一个数n,返回小于n的素数个数. 思路:设数字 k =from 2 to sqrt(n),那么对于每个k,从k2开始,在[2,n)范围内只要是k的倍数的都删掉(也就是说[k,k2)是不用理的, ...

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

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

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

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

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

  9. 204. Count Primes - LeetCode

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

随机推荐

  1. angularjs 指令—— 绑定策略(@、=、&)

    angularjs 指令—— 绑定策略(@.=.&) 引入主题背景:angular 的指令配置中的template可以直接使用硬编码写相应的代码,不过也可以根据变量,进行动态更新.那么需要用到 ...

  2. 简单代码在ABAP中实现声音的播放

    这段代码的功能是在SAP里面实现声音的播放,可以用作程序提醒功能,和SAP里面’噹噹噹’那个声音的意思差不多.将来在项目中遇到客户想要SAP ABAP发出一点声音的时候就可以参考一下这个程序. REP ...

  3. 【Leafletjs】7.结合echart图表展示信息

    1.popup中添加图表信息 //定义marker var marker = L.marker(val.location).addTo(map); var content = '<div sty ...

  4. 源码详解openfire保存消息记录_修改服务端方式

    实现openfire消息记录通常有两种方式,修改服务端和添加消息记录插件. 今天,简单的说明一下修改服务端方式实现消息记录保存功能. 实现思路 修改前: 默认的,openfire只提供保存离线记录至o ...

  5. Android Animation学习(四) ApiDemos解析:多属性动画

    Android Animation学习(四) ApiDemos解析:多属性动画 如果想同时改变多个属性,根据前面所学的,比较显而易见的一种思路是构造多个对象Animator , ( Animator可 ...

  6. Ubuntu下安装Naginx, PHP5(及PHP-FPM),MySQL

    一:安装前做个简单的说明 二:安装MySQL 三:安装Nginx 四:安装PHP5 五:配置 nginx,以下是我本机的配置文件. 六:让MySQL支持PHP5 七:配置PHP-FPM 八:在/etc ...

  7. 简单粗暴的对android so文件加壳,防止静态分析

    转载自http://bbs.pediy.com/showthread.php?t=191649 以前一直对.so文件加载时解密不懂,不了解其工作原理和实现思路.最近翻看各种资料,有了一些思路.看到论坛 ...

  8. 【代码笔记】iOS-城市plist

    一,效果图. 二,工程图. 三,代码. RootViewController.h #import <UIKit/UIKit.h> @interface RootViewController ...

  9. ajax async

    $.post("index.php?app=default&act=ajaxBigImage", {goods_id: goods_id},function(data){$ ...

  10. 怎么样使用yum来安装、卸载jdk

    安装好的CentOS会自带OpenJdk,用命令 java -version ,会有下面的信息: java version "1.6.0"OpenJDK  Runtime Envi ...