Integer to Roman
Given an integer, convert it to a roman numeral.
Input is guaranteed to be within the range from 1 to 3999.
Method
Be careful, the Dict is no order but List has order. So we need use the List to do this question.
Python Code
class Solution(object):
def intToRoman(self, num):
"""
:type num: int
:rtype: str
"""
nums = [1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1]
strings = ["M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"]
result = ''
for i in range(len(nums)):
while num >= nums[i]:
num -= nums[i]
result += strings[i]
return result
近期评论