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 ...
随机推荐
- go语言 类型:数组切片
初看起来,数组切片就像一个指向数组的指针,实际上它拥有自己的数据结构,而不仅仅是个指针.数组切片的数据结构可以抽象为以下3个变量: 1.一个指向原生数组的指针: 2.数组切片中的元素个数: 3.数组切 ...
- hibernate 入门([数据访问中间件] 开源框架)
1.内容: hibernate 也是一个经典的[数据访问中间件] 开源框架. 2.hibernate核心组件 SessionFactory[整个数据的操作]重量级组件 ...
- 利用JSONP实现跨域请求
前言:有时候一忙起来就没了时间观念,原来我已经有十多天没写博客了.一直想做跨域方面的尝试,无奈最近准备校招没时间动动手.今天就先讲讲JSONP吧,昨晚还在研究QQ空间日志里面网络图片的问题呢,我发现日 ...
- 如何:在 SharePoint 中创建外部列表
在创建外部内容类型后创建外部列表是一项非常简单的任务,有如下4种方式进行: 可使用 Microsoft SharePoint Designer 2010 浏览器来完成 VS2010的列表实例 采用代码 ...
- SharePoint 部署解决方案Feature ID冲突
中文报错: 部署步骤“添加解决方案”中出现错误: 已在此服务器场中安装 ID 为 735efe4e-8b50-4310-b588-c6ae2ba0759f 的功能.请使用强制属性显式地重新安装此功能. ...
- Mac OX上安装MongoDb
MongoDB的安装有好多种安装方法,有普通青年的HomeBrew方式,也有文艺青年的源码编译方式.我只想快速的装起来用一下,所以我选最简单的HomeBrew. 请参考官方文档 : http://do ...
- Round in Oracle/VBA
VBA的 Round采用的是银行家算法(rounds to the nearest even number) Round(1.5) = 2 Round(0.5) = 在Oracle中实现银行家算法 S ...
- Large-file-chunk-size 设置最大文件上传值
Large-file-chunk-size: Stsadm property (Office SharePoint Server) SharePoint 2007 0 out of 1 rated ...
- git使用详细介绍
1. Git概念 1.1. Git库中由三部分组成 Git 仓库就是那个.git 目录,其中存放的是我们所提交的文档索引内容,Git 可基于文档索引内容对其所管理的文档进行内容追踪,从而 ...
- 普通View的measure流程
对于普通的view,其测量在ViewGroup中的measureChildWithMargins函数中调用child view的measure开始测量. 1:从measure函数开始 public f ...