204. Count Primes

来源:互联网 发布:淘宝规定卖家发货时间 编辑:程序博客网 时间:2024/06/05 05:41

题目,
python3 code:

class Solution(object):    def countPrimes(self, n):        """        :type n: int        :rtype: int        """        if n<2:            return 0        r = [1]*(n-2)        count = 0        i = 2        while i < n:            multi = i            # print(start)            if r[i-2] == 1:                while i*multi <= n-1:                # print(start*multi,end='\t')                    r[i*multi-2] = 0                    multi += 1                count += 1            i += 1            # print(count)        return count

accepted!,时间1978ms,16.97%。
这里写图片描述
if [i-2] == 1改为if [i-2]之后,耗时1498ms,31.81%.
这里写图片描述

原创粉丝点击