题目
题目的意思是返回第n个ugly number ,比如1,2,3,4,5,6,8,9,10,12,…
解答
class Solution(object):
def nthUglyNumber(self, n):
"""
:type n: int
:rtype: int
"""
nums = [0,1]
x,y,z = 1,1,1
while len(nums)<n+1:
m1, m2, m3 = 2*nums[x], 3*nums[y], 5*nums[z]
m = min(m1,m2,m3)
nums.append(m)
if m==m1:
x+=1
if m==m2:
y+=1
if m==m3:
z+=1
return nums[-1]
这一类的基本上都可以这样做.





近期评论