-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprob_12.py
26 lines (22 loc) · 911 Bytes
/
prob_12.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
# 12. Integer to Roman
# https://leetcode.com/problems/integer-to-roman/
class Solution(object):
def intToRoman(self, num):
"""
:type num: int
:rtype: str
"""
M = ["", "M", "MM", "MMM"];
C = ["", "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM"];
X = ["", "X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC"];
I = ["", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX"];
return M[num//1000] + C[(num%1000)//100] + X[(num%100)//10] + I[num%10]
strs = ['M', 'CM', 'D', 'CD', 'C', 'XC', 'L', 'XL', 'X', 'IX', 'V', 'IV', 'I']
nums = [1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1]
ret = ""
for i, j in enumerate(nums):
while num >= j:
ret += strs[i]
num -= j
if num == 0:
return ret