class Solution(object):
def intToRoman(self, num):
"""
:type num: int
:rtype: str
"""
tmp = []
roman = ''
if not num:
raise ValueError
while num >= 1000:
num -= 1000
tmp.append('M')
while num >= 900:
num -= 900
tmp.append('CM')
while num >= 500:
num -= 500
tmp.append('D')
while num >= 400:
num -= 400
tmp.append('CD')
while num >= 100:
num -= 100
tmp.append('C')
while num >= 90:
num -= 90
tmp.append('XC')
while num >= 50:
num -= 50
tmp.append('L')
while num >= 40:
num -= 40
tmp.append('XL')
while num >= 10:
num -= 10
tmp.append('X')
while num >= 9:
num -= 9
tmp.append('IX')
while num >= 5:
num -= 5
tmp.append('V')
while num >= 4:
num -= 4
tmp.append('IV')
while num >= 1:
num -= 1
tmp.append('I')
return roman.join(tmp)
class Solution(object):
def intToRoman(self, num):
"""
:type num: int
:rtype: str
"""
tmp = []
hashmap = {'I': 1, 'IV': 4, 'IX': 9, 'XL': 40, 'XC': 90, 'CD': 400, 'CM': 900, 'V': 5,
'X': 10, 'L': 50, 'C': 100, 'D': 500, 'M': 1000}
sort_hashmap = sorted(hashmap.items(), key=lambda _: _[1], reverse=True)
roman = ''
for i in range(len(sort_hashmap)):
while num>=sort_hashmap[i][1]:
num -= sort_hashmap[i][1]
tmp.append(sort_hashmap[i][0])
return roman.join(tmp)