forked from criszhou/LeetCode-Python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path012. Integer to Roman.py
48 lines (43 loc) · 1.09 KB
/
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
class Solution(object):
def intToRoman(self, num):
"""
:type num: int
:rtype: str
"""
retList = [];
while num >= 1000:
retList.append('M');
num -= 1000
if num >= 900:
retList.append('CM');
num -= 900
if num >= 500:
retList.append('D');
num -= 500
if num >= 400:
retList.append('CD');
num -= 400
retList.append('C' * (num // 100))
num %= 100
if num >= 90:
retList.append('XC');
num -= 90
if num >= 50:
retList.append('L');
num -= 50
if num >= 40:
retList.append('XL');
num -= 40
retList.append('X' * (num // 10))
num %= 10
if num >= 9:
retList.append('IX');
num -= 9
if num >= 5:
retList.append('V');
num -= 5
if num >= 4:
retList.append('IV');
num -= 4
retList.append('I' * num)
return "".join(retList);