Split each number into 4 parts [a, b, c, d]. Where a is the number of -1, b is the number of 5, c is the number of 1, d is the number of 10.
class Solution:
def intToRoman(self, num: int) -> str:
m1 = ["I", "X", "C", "M", ""]
m5 = ["V", "L", "D", "", ""]
shift = 0
res = ''
while num:
a, b, c, d = self._get_parts(num % 10)
res = m1[shift] * a + m5[shift] * b + m1[shift] * c + m1[shift + 1] * d + res
num //= 10
shift += 1
return res
def _get_parts(self, num):
mapping = [
(0, 0, 0, 0),
(0, 0, 1, 0),
(0, 0, 2, 0),
(0, 0, 3, 0),
(1, 1, 0, 0),
(0, 1, 0, 0),
(0, 1, 1, 0),
(0, 1, 2, 0),
(0, 1, 3, 0),
(1, 0, 0, 1)
]
return mapping[num]