Python solution time limited exceeded?


  • 0
    Z

    here is my code:

    class Solution:
        # @param {integer} n
        # @return {integer}
        def countPrimes(self, n):
            self.n = n
            if n <= 1:
                primes = []
            else:
                numbers = set(xrange(n,1,-1))
                primes = []
                for i in xrange(int(round(max(numbers)**(0.5)))):
                    p = numbers.pop()
                    primes.append(p)
                    numbers.difference_update(set(xrange(p*2,n+1,p)))
                primes.extend(numbers)
            
            a = len(primes)
            return a
    

    Can any one help me?


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.