leetcode-Count Primes 以及python的小特性
题目大家都非常熟悉,求小于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的小特性的更多相关文章
- [leetcode] Count Primes
Count Primes Description: Count the number of prime numbers less than a non-negative number, n click ...
- [LeetCode] Count Primes 质数的个数
Description: Count the number of prime numbers less than a non-negative number, n click to show more ...
- leetcode Count and Say python
class Solution(object): def countAndSay(self, n): """ :type n: int :rtype: str " ...
- Python3解leetcode Count Primes
问题描述: Count the number of prime numbers less than a non-negative number, n. Example: Input: 10 Outpu ...
- LeetCode Count Primes 求素数个数(埃拉托色尼筛选法)
题意:给一个数n,返回小于n的素数个数. 思路:设数字 k =from 2 to sqrt(n),那么对于每个k,从k2开始,在[2,n)范围内只要是k的倍数的都删掉(也就是说[k,k2)是不用理的, ...
- [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 ...
- 【刷题-LeetCode】204. Count Primes
Count Primes Count the number of prime numbers less than a non-negative number, *n*. Example: Input: ...
- 204. Count Primes - LeetCode
Queston 204. Count Primes Solution 题目大意:给一个数,求小于这个数的素数的个数 思路:初始化一个boolean数组,初始设置为true,先遍历将2的倍数设置为fal ...
随机推荐
- win7系统下,vs2010一调式,vs就关闭要重启
进入我的文档 %appdata%\Microsoft\VisualStudio, 将 10.0 重命名.网上找的方法有些问题,可能找这路径很难找到啊. 于是自己 找了找 一般都在当前用户文件夹下 Ap ...
- 在不知下面有几个元素的时候,要去除最后一个元素的下边框jquery代码
<script> $(document).ready(function() { $(".search_list dl").each(function() {//遍历所有 ...
- Android NDK环境搭建及调用JNI的简单步骤
转载请注明:http://www.cnblogs.com/tiantianbyconan/p/3396595.html Java Native Interface (JNI)标准是java平台的一部分 ...
- 导致VC不能释放的几个原因
delegate的属性不是weak NSTimer没有invalidate block中的强引用 调用了performSelector,退出时没有cancelPerformSelectorsWithT ...
- Android Handler机制(三)----Looper源码解析
一.Looper Looper对象,顾名思义,直译过来就是循环的意思,从MessageQueue中不断取出message. Class used to run a message loop for a ...
- android加固系列—5.加固前先学会破解,hook(钩子)jni层系统api
[版权所有,转载请注明出处.出处:http://www.cnblogs.com/joey-hua/p/5138585.html] crackme项目jni的关键代码(项目地址见文章底部),获取当前程序 ...
- 【iOS】使用CoreText实现图文混排
iOS没有现成的支持图文混排的控件,而要用多个基础控件组合拼成图文混排这样复杂的排版,是件很苦逼的事情.对此的解决方案有使用CoreText进行绘制,或者使用TextKit.本文主要讲解对于CoreT ...
- Xcode快捷键大全
转载地址http://www.360doc.com/content/12/0521/09/6541311_212458595.shtml.
- 【Android】神奇的android:clipChildren属性
前言 前几天有在微博上推荐过一个博客,看他文章时发现了这个属性.有些属性不常用,但需要的时候非常有用,于是做了个例子,正好项目用到,与大家分享一下. 声明 欢迎转载,请注明出处! 博客园:http:/ ...
- 结对编程-地铁续(有种上个学期OO的既视感)
我们组比较特殊..三人结对 github:https://github.com/qingchanghan/WPFUI_Metro po一张照片: 石浩然,韩青长.陈彦吉 (台式机真的很高端,分屏贼帅) ...