Task13罗马数字转整数

#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

#将罗马数字保存在数组里,然后通过规律取值相加就行了。

发布了13 篇原创文章 · 获赞 0 · 访问量 185

猜你喜欢

转载自blog.csdn.net/crazyseven__/article/details/104833587