Saturday, March 9, 2019

LeetCode 204. Count Primes

'''
Count the number of prime numbers less than a non-negative number, n.

'''
#1248ms, 69.9MB
class Solution(object):
    def countPrimes(self, n):
        """
        :type n: int
        :rtype: int
        """
        nl = [i for i in range(n)]
       
        for i in nl:
            if i > 1:
                j = i*2
                while j < n:
                    if not nl[j] == 1: nl[j] = 1
                    j += i
                   
        result = [k for k in nl if k > 1]
        return len(result)

No comments:

Post a Comment