-
Notifications
You must be signed in to change notification settings - Fork 0
/
12_integer_to_roman.py
54 lines (52 loc) · 1.44 KB
/
12_integer_to_roman.py
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
'''
Given an integer, convert it to a roman numeral.
Input is guaranteed to be within the range from 1 to 3999.
'''
class Solution(object):
def intToRoman(self, num):
"""
:type num: int
:rtype: str
"""
result = []
while num > 0:
if num >= 1000:
result.append('M')
num -= 1000
elif num >= 900:
result.append('CM')
num -= 900
elif num >= 500:
result.append('D')
num -= 500
elif num >= 400:
result.append('CD')
num -= 400
elif num >= 100:
result.append('C')
num -= 100
elif num >= 90:
result.append('XC')
num -= 90
elif num >= 50:
result.append('L')
num -= 50
elif num >= 40:
result.append('XL')
num -= 40
elif num >= 10:
result.append('X')
num -= 10
elif num >= 9:
result.append('IX')
num -= 9
elif num >= 5:
result.append('V')
num -= 5
elif num >= 4:
result.append('IV')
num -= 4
else:
result.append('I')
num -= 1
return ''.join(result)