工作生活

丑数 263, 264, 313

2019-07-01  本文已影响0人  poteman

用一套代码解决:

temp = []
for j in range(len(exp)):
    temp.append(res[exp[j]] * primes[j])
cur = min(temp)
res.append(cur)
for j in range(len(exp)):
    if cur == res[exp[j]] * primes[j]:
        exp[j] += 1
class Solution(object):
    def isUgly(self, num):
        """
        :type num: int
        :rtype: bool
        """
        if num <= 0:
            return False
        if num == 1:
            return True
        
        exp = [0, 0, 0]
        primes = [2, 3, 5]
        res = [1]
        
        while res[-1] < num:
            temp = []
            for i in range(len(exp)):
                temp.append(res[exp[i]] * primes[i])
                
            cur = min(temp)
            res.append(cur)
            
            for i in range(len(exp)):
                if cur == res[exp[i]] * primes[i]:
                    exp[i] += 1
            
        return res[-1] == num
def nthSuperUglyNumber(self, n, primes):
        """
        :type n: int
        :type primes: List[int]
        :rtype: int
        """
        res = [1]
        exp = [0] * len(primes)
        
        for i in range(1, n):
            temp = []
            for j in range(len(exp)):
                temp.append(res[exp[j]] * primes[j])
            cur = min(temp)
            res.append(cur)
            for j in range(len(exp)):
                if cur == res[exp[j]] * primes[j]:
                    exp[j] += 1
        return res[-1]
上一篇下一篇

猜你喜欢

热点阅读