Simple python solution - using set


  • 0
    S
        def nthUglyNumber(self, n):
            """
            :type n: int
            :rtype: int
            """
            
            minx = 1
            n = n - 1
            
            q = {2,3,5}
            while n:
                minx = min(q)
                q.remove(minx)
                q.add(2*minx)
                q.add(3*minx)
                q.add(5*minx)
                n -= 1
            
            return minx

Log in to reply
 

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