-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathleetcode-12-Integer_to_Roman.py
45 lines (40 loc) · 1.19 KB
/
leetcode-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
class Solution:
def intToRoman(self, num: int) -> str:
roman_vals = {
1:'I',
5:'V',
4:'IV',
9:'IX',
10:'X',
40:'XL',
50:'L',
90:'XC',
100:'C',
400:'CD',
500:'D',
900:'CM',
1000:'M'
}
num_lst: list = [int(c) for c in str(num)]
res: str = ''
for i in range(len(num_lst)):
power_val = ((10**(len(num_lst)-(i+1))))
cur_val = num_lst[i]*power_val
if cur_val in roman_vals.keys():
res += roman_vals[cur_val]
num_lst[i] -= 0
else:
while num_lst[i] != 0:
if 5*power_val<cur_val:
num_lst[i] -= 5
res += roman_vals[5*power_val]
else:
num_lst[i] -= 1
res += roman_vals[power_val]
cur_val = num_lst[i]*power_val
return res
if __name__ == "__main__":
num: int = 3749
# num: int = 1111
res: str = Solution().intToRoman(num)
print(res)