leetcode -. 13 Roman numerals to Integer

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]]
                    in + 2 =
                 presence :
                    v+=memo[s[i]]
                    in + 1 =
             presence :
                v+=memo[s[i]]
                i+=1
        return v
When execution: 72 ms, beat the 65.93% of users in all python3 submission
Memory consumption: 14.1 MB, beat the 5.25% of users in all python3 submission
 
 
                                                                        ——2019.10.17

Guess you like

Origin www.cnblogs.com/taoyuxin/p/11693038.html