zoukankan      html  css  js  c++  java
  • 剑指Offer 49 丑数

    丑数

    把只包含质因子2、3和5的数称作丑数(Ugly Number)。例如6、8都是丑数,但14不是,因为它包含质因子7。 习惯上我们把1当做是第一个丑数。求按从小到大的顺序的第N个丑数。

     1 # -*- coding:utf-8 -*-
     2 class Solution:
     3     def GetUglyNumber_Solution(self, index):
     4         if index <= 6:
     5             return index
     6         i2,i3,i5= 0,0,0
     7         dp = [0] * index
     8         dp[0] = 1
     9         for i in range(1,index):
    10             next2 = dp[i2] * 2
    11             next3 = dp[i3] * 3
    12             next5 = dp[i5] * 5
    13             dp[i] = min(next2,min(next3,next5))
    14             if dp[i] == next2:
    15                 i2 += 1
    16             if dp[i] == next3:
    17                 i3 += 1
    18             if dp[i] == next5:
    19                 i5 += 1
    20         return dp[index-1]
    21         # write code here
  • 相关阅读:
    angular 个人零点学习
    angularjs 五大关键点
    OA项目学习总结
    oa
    时间插件
    angular js模态框
    搜索
    xianduanshu
    o-o
    paibingbuzhen
  • 原文地址:https://www.cnblogs.com/asenyang/p/11022941.html
Copyright © 2011-2022 走看看