leetcode313-- Super Ugly Number.

leetcode313-- Super Ugly Number.

题目

题目的意思是返回第n个ugly number ,比如1,2,3,4,5,6,8,9,10,12,… 只不过这时候更通用一些,并没有给primes是什么. 解答


class Solution(object):
    def nthSuperUglyNumber(self, n, primes):
        """
        :type n: int
        :type primes: List[int]
        :rtype: int
        """
        primes.sort()
        num = len(primes)
        
        nums = [0,1]
        
        idxs = [1]*num
        
        while len(nums) < n+1:
         
            temps = [nums[idxs[i]]*primes[i] for i in range(num)]
            
            cur = min(temps)
            
            for j, temp in enumerate(temps):
                if cur == temp:
                    idxs[j] += 1
                    
            nums.append(cur)
        print(nums) 
        return nums[-1]

打赏,谢谢~~

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,多谢支持~

打开微信扫一扫,即可进行扫码打赏哦