Integer to Roman LeetCode solution in Python

1
# Runtime: 47 ms (Top 86.4%) | Memory: 16.42 MB (Top 14.9%)
2

3

4
class Solution:
5
def intToRoman(self, num: int) -> str:
6
# Creating Dictionary for Lookup
7
num_map = {
8
1: "I",
9
5: "V",
10
4: "IV",
11
10: "X",
12
9: "IX",
13
50: "L",
14
40: "XL",
15
100: "C",
16
90: "XC",
17
500: "D",
18
400: "CD",
19
1000: "M",
20
900: "CM",
21
}
22

23
# Result Variable
24
r = ""
25

26
for n in [1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1]:
27
# If n in list then add the roman value to result variable
28
while n <= num:
29
r += num_map[n]
30
num -= n
31
return r

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0