美文网首页工作生活
丑数 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

    用一套代码解决: 263 313

  • 每周 ARTS 第 11 期

    1. Algorithm 263. 丑数(简单) 描述: 编写一个程序判断给定的数是否为丑数。丑数就是只包含质因数...

  • 313:超级丑数

    题意 超级丑数是一个正整数,并满足其所有质因数都出现在质数数组 primes 中。给你一个整数 n 和一个整数数组...

  • 263. 丑数

    内容 编写一个程序判断给定的数是否为丑数。 丑数就是只包含质因数 2, 3, 5 的正整数。 示例 1: 输入: ...

  • 263. 丑数

    题目 分析 所谓一个数m是另一个数n的因子,是指n能被m整除,也就是n % m == 0。根据丑数的定义,丑数只能...

  • 263、丑数(E)

    判断一个正整数是否为一个丑数。丑数的定义是 1 为丑数,只包含 2、3、5的数就是丑数,比如 4,8,但是14 就...

  • 263. 丑数

    题目描述 编写一个程序判断给定的数是否为丑数。 丑数就是只包含质因数 2, 3, 5 的正整数。 示例: 思路 1...

  • Leetcode 263 丑数

    丑数 题目 编写一个程序判断给定的数是否为丑数。 丑数就是只包含质因数 2, 3, 5 的正整数。 示例1:输入:...

  • 263. 丑数

    编写一个程序判断给定的数是否为丑数。 丑数就是只包含质因数 2, 3, 5 的正整数。 示例 1: 输入: 6输出...

  • 263-丑数

    编写一个程序判断给定的数是否为丑数。丑数就是只包含质因数2, 3, 5的正整数。 既然质因子只有2、3、5,那么就...

网友评论

    本文标题:丑数 263, 264, 313

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