1. 程式人生 > >leetcode263 & 264醜數

leetcode263 & 264醜數

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