forked from algorhythms/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
012 Integer to Roman.py
45 lines (36 loc) · 1011 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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
"""
Given an integer, convert it to a roman numeral.
Input is guaranteed to be within the range from 1 to 3999.
"""
__author__ = 'Danyang'
int2roman = {
1: "I",
4: "IV",
5: "V",
9: "IX",
10: "X",
40: "XL",
50: "L",
90: "XC",
100: "C",
400: "CD",
500: "D",
900: "CM",
1000: "M"
}
class Solution:
def intToRoman(self, num):
"""
dealing with digits
notice the 1, 4, 5, 9, 10 repetition
:param num: integer, Input is guaranteed to be within the range from 1 to 3999
:return: a string
"""
string_builder = []
components = [1, 4, 5, 9, 10, 10, 40, 50, 90, 100, 400, 500, 900, 1000]
# starting from the largest
for component in reversed(components): # reversed
while num >= component:
string_builder.append(int2roman[component])
num -= component
return "".join(string_builder)