美文网首页工作生活
丑数 263, 264, 313

丑数 263, 264, 313

作者: poteman | 来源:发表于2019-07-01 23:27 被阅读0次

    用一套代码解决:

    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

        本文链接:https://www.haomeiwen.com/subject/uxsocctx.html