leetcode python 13. 罗马数字转整数

https://leetcode-cn.com/problems/roman-to-integer/description/
这题就是做简单的模拟,用一个Dict做一个映射,关键点是:判断当前数如果小于下一个数,则执行下一个数减当前数的操作。

class Solution(object):
    def romanToInt(self, s):
        """
        :type s: str
        :rtype: int
        """
        D = {"I":1,"V":5,"X":10,"L":50,"C":100,"D":500,"M":1000}
        sum = 0
        i = 0
        while(i<=(len(s)-1)):
            if(i!=len(s)-1):
                if(D[s[i]]1]]):
                   sum = sum +(D[s[i+1]]-D[s[i]])
                   i=i+1
                else:
                    sum = sum + D[s[i]]
            else:
                sum = sum + D[s[i]]
            i=i+1
        return sum

现在都在做简单题,我不知道能坚持多久,半途而废,至少也走过半途吧……..

你可能感兴趣的:(Leetcode)