丑数 263, 264, 313

用一套代码解决:

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
  • 263
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
  • 313
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]

你可能感兴趣的:(丑数 263, 264, 313)