-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path012_integer-to-roman.py
28 lines (26 loc) · 1013 Bytes
/
012_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
class Solution(object):
def intToRoman(self, num):
"""
:type num: int
:rtype: str
"""
roman_num = ""
roman_dict = {1: 'I', 5: 'V', 10: 'X', 50: 'L', 100: 'C', 500: 'D', 1000: 'M'}
dec_roman_list = [1000, 500, 100, 50, 10, 5, 1]
roman_index = 0
max_roman = dec_roman_list[roman_index]
while num:
if num >= max_roman:
roman_num += roman_dict[max_roman]
num = num - max_roman
else:
roman_index += 1
max_roman = dec_roman_list[roman_index]
roman_num = roman_num.replace("DCCCC", "CM") # 900
roman_num = roman_num.replace("CCCC", "CD") # 400
roman_num = roman_num.replace("LXXXX", "XC") # 90
roman_num = roman_num.replace("XXXX", "XL") # 40
roman_num = roman_num.replace("VIIII", "IX") # 9
roman_num = roman_num.replace("IIII", "IV") # 4
return roman_num
print(Solution().intToRoman(149))