forked from apachecn/Interview
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path33-丑数.py
More file actions
executable file
·30 lines (22 loc) · 891 Bytes
/
33-丑数.py
File metadata and controls
executable file
·30 lines (22 loc) · 891 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# -*- coding:utf-8 -*-
class Solution:
def GetUglyNumber_Solution(self, index):
# write code here
if not index:
return 0
ugly_number = [1]*index
next_index = 1
index2 = 0
index3 = 0
index5 = 0
while next_index < index:
minValue = min(ugly_number[index2]*2, ugly_number[index3]*3,ugly_number[index5]*5)
ugly_number[next_index] = minValue
while ugly_number[index2]*2 <= ugly_number[next_index]:
index2 += 1
while ugly_number[index3]*3 <= ugly_number[next_index]:
index3 += 1
while ugly_number[index5]*5 <= ugly_number[next_index]:
index5 += 1
next_index += 1
return ugly_number[-1]