LeetCode4——罗马数字转整数
前言:
题目内容:
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/roman-to-integer
题目解法:
class Solution(object):
def romanToInt(self, s):
"""
:type s: str
:rtype: int
"""
a=['I', 'V', 'X', 'L', 'C', 'D', 'M']
b=[1,5,10,50,100,500,1000]
c=zip(a,b)
num=dict((a,b) for a,b in c)
roman=0
for i in range(len(s)):
if i < len(s) - 1 and num[s[i]] < num[s[i + 1]]:
roman -= num[s[i]]
else:
roman += num[s[i]]
return roman
if __name__ == '__main__':
x='MCMXCIVI'
a=Solution()
print(a.romanToInt(x))