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 ...
随机推荐
- [deviceone开发]-拼图小游戏
一.简介 九宫格小游戏,可从本地图库载入一张图片,填充到9个ImageView,另涉及Timer计时.图库控件. 每个格子都是相同的控件,动态添加到首页中的,在初始化后,响应touch事件,之后通过多 ...
- easyui加载datagrid时随着窗体大小改变而改变
function initTable() { $('#tt').datagrid({ width: $(document).width() - 20, heig ...
- JavaScript实战(原生range和自定义特效)
今天我又码了两个特效:一个是用原生input[type=range]的,另一个完全自定义的:下面是完整代码和演示: #tip{ position: absolute; top: 30px; left: ...
- ios开发人员北京,上海,深圳的工资待遇是多少?
ios开发人员北京,上海,深圳的工资待遇是多少? [1]首先看看平均工资 从图中来看,北京平均工资15570 居首,不愧是首都啊. 你过了平均线了吗?是不是感觉被平均了,如果感觉工资 ...
- iOS开发-生成随机数
有时候我们需要在程序中生成随机数,但是在Objective-c中并没有提供相应的函数,好在C中提供了rand().srand().random().arc4random()几个函数.那么怎么使用呢?下 ...
- 【转】JavaScript 异步进化史
前言 JS 中最基础的异步调用方式是 callback,它将回调函数 callback 传给异步 API,由浏览器或 Node 在异步完成后,通知 JS 引擎调用 callback.对于简单的异步操作 ...
- php示例代码之读取文件
php读取文件 1 2 3 4 5 6 7 8 $sourceString=''; $fp = @fopen($filename, "r"); while($line = ...
- stringstream操纵string小总结
1 split字符串 之前在用C#写代码的时候,用过split函数,可以把一个字符串根据某个分隔符分成若干个字符串数组.在用C++操纵字符串的时候,我一直使用很笨的遍历的方法.为此,我问候过很多次C+ ...
- Web API与国际化
软件国际化是在软件设计和文档开发过程中,使得功能和代码设计能处理多种语言和文化习俗,在创建不同语言版本时,不需要重新设计源程序代码的软件工程方法.这在很多成熟的软件开发平台中非常常见.对于.net开发 ...
- Mongodb Manual阅读笔记:CH8 复制集
8 复制 Mongodb Manual阅读笔记:CH2 Mongodb CRUD 操作Mongodb Manual阅读笔记:CH3 数据模型(Data Models)Mongodb Manual阅读笔 ...