https://leetcode.com/problems/integer-to-roman/?envType=featured-list&envId=top-google-questions
input :
output :
조건 :
I
can be placed before V
(5) and X
(10) to make 4 and 9. X
can be placed before L
(50) and C
(100) to make 40 and 90. C
can be placed before D
(500) and M
(1000) to make 400 and 900.Solution explain : Solution1
IV
, IX
와 같은 4, 9를 나타내는 값들을 딕셔너리로 표현하자.class Solution:
def intToRoman(self, num: int) -> str:
ret = ""
temp = {
"I" : 1, "IV" : 4,
"V" : 5, "IX" : 9,
"X" : 10, "XL" : 40,
"L" : 50, "XC" : 90,
"C" : 100, "CD" : 400,
"D" : 500, "CM" : 900,
"M" : 1000,
}
keys = list(temp.keys())[::-1]
for item in keys:
divide = num // temp[item]
ret += item * divide
num %= temp[item]
return ret
# s = Solution()
# print(s.intToRoman(3))
# print(s.intToRoman(58))
# print(s.intToRoman(1994))