#leetcode题
class Solution(object):
def romanToInt(self, s):
"""
:type s: str
:rtype: int
"""
sum1=0
list1={'I':1,'V':5,'X':10,'L':50,'C':100,'D':500,'M':1000}
for i in range(0,len(s)) :
if i<len(s)-1 and list1[s[i]]<list1[s[i+1]]:
sum1-=list1[s[i]]
else:
sum1+=list1[s[i]]
return sum1
#将罗马数字保存在数组里,然后通过规律取值相加就行了。