题目:
Given an integer, convert it to a roman numeral.
Input is guaranteed to be within the range from 1 to 3999.
题意:
把数字转换成罗马数字
数字与罗马数字相互表示的原则:
方法一:119ms
class Solution(object):
def intToRoman(self, num):
"""
:type num: int
:rtype: str
"""
c = [['','I','II','III','IV','V','VI','VII','VIII','IX'],
['','X','XX','XXX','XL','L','LX','LXX','LXXX','XC'],
['','C','CC','CCC','CD','D','DC','DCC','DCCC','CM'],
['','M','MM','MMM']]
s = []
i = 3
while num :
s.append(c[i][num/pow(10, i)])
num %= pow(10, i)
i -= 1
return ''.join(s)
方法二:99ms
class Solution(object):
def intToRoman(self, num):
"""
:type num: int
:rtype: str
"""
M = ["", "M", "MM", "MMM"]
C = ["", "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM"]
X = ["", "X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC"]
I = ["", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX"]
return M[num/1000] + C[(num%1000)/100] + X[(num%100)/10] + I[num%10]