forked from neozl24/leecode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
integer_to_roman_.py
32 lines (30 loc) · 1.01 KB
/
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
class Solution:
# @return a string
def intToRoman(self, num):
mapping = {0:'', 1:'I', 5:'V', 10:'X', 50:'L', 100:'C', 500:'D', 1000:'M'}
units = [1000, 100, 10, 1]
roman = ''
for i in units:
count = num // i
if count == 2:
roman += mapping[i] + mapping[i]
elif count == 3:
roman += mapping[i] + mapping[i] * 2
elif count == 4:
roman += mapping[i] + mapping[5*i]
elif count == 6:
roman += mapping[5*i] + mapping[i]
elif count == 7:
roman += mapping[5*i] + mapping[i] * 2
elif count == 8:
roman += mapping[5*i] + mapping[i] * 3
elif count == 9:
roman += mapping[i] + mapping[10*i]
else:
roman += mapping[count*i]
num = num % i
return roman
if __name__ == '__main__':
test = Solution()
print test.intToRoman(3999)
raw_input()