264. Ugly Number II

class Solution(object):
    def nthUglyNumber(self, n):
        """
        :type n: int
        :rtype: int
        """
        h=[(1,1)]
        for _ in xrange(n):
            val,fact=heapq.heappop(h)
            for x in [2,3,5]:
                if fact<=x:
                    heapq.heappush(h,(val*x,x))
        return val

你可能感兴趣的:(264. Ugly Number II)