leetcode——13. 罗马数字转整数

class Solution:
    def romanToInt(self, s: str) -> int:
        memo={'I':1,'IV':4,'V':5,'IX':9,'X':10,'XL':40,'L':50,'XC':90,'C':100,'CD':400,'D':500,'CM':900,'M':1000}
        i=0
        v=0
        while i<len(s):
            if i<len(s)-1 and s[i] in ['I','X','C']:
                if s[i:i+2] in ['IV','IX','XL','XC','CD','CM']:
                    v+=memo[s[i:i+2]]
                    i+=2
                else:
                    v+=memo[s[i]]
                    i+=1
            else:
                v+=memo[s[i]]
                i+=1
        return v
执行用时 :72 ms, 在所有 python3 提交中击败了65.93%的用户
内存消耗 :14.1 MB, 在所有 python3 提交中击败了5.25%的用户
 
 
                                                                        ——2019.10.17

猜你喜欢

转载自www.cnblogs.com/taoyuxin/p/11693038.html