leetcode263 & 264丑数

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u014204761/article/details/82940601
leetcode264
def nthUglyNumber(self, n):
        """
        找到前n个丑数,每次新增到丑数比之前丑数数组到最大值都大,保证这一点,再确保每个与2 3 5 相        
        乘的数不重复,定义三个指针。
        """
        if n == 1:
            return 1
        arr = [1]
        t1 = 0
        t2 = 0
        t3 = 0
        while len(arr) < n:
            a,b,c = 2*arr[t1],3*arr[t2],5*arr[t3]
            arr.append(min(a,b,c))
            if arr[-1] == a:
                t1 += 1
            if arr[-1] == b:
                t2 += 1
            if arr[-1] == c:
                t3 += 1
        return arr[-1]
leetcode263
def isUgly(self, num):
        '''
          
        '''
        while num > 1:
            if int(num%2) == 0:
                num = int(num/2)
            elif int(num%3) == 0:
                num = int(num/3)
            elif int(num%5) == 0:
                num = int(num/5)
            else:
                break
        if num == 1:
            return True
        else:
            return False

猜你喜欢

转载自blog.csdn.net/u014204761/article/details/82940601