题目:13. Roman to Integer
链接:https://leetcode.com/problems/roman-to-integer/description/
把上一个题目的代码拿来用了。
romanDict = [("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", "", "", "", "", "", "")]
def intToRoman(num):
if not str(num):
return ""
return romanDict[len(str(num)) - 1][int(str(num)[0]) - 1] + intToRoman(str(num)[1:len(str(num))])
class Solution(object):
dic = {}
for i in range(3999):
dic[intToRoman(i + 1)] = i + 1
def romanToInt(self, s):
"""
:type s: str
:rtype: int
"""
return self.dic[s]