Roman to Integer Leetcode Python Solution



 class Solution:
    def romanToInt(self, s: str) -> int:
        roman = {'I':1,
                'V':5,
                'X':10,
                'L':50,
                'C':100,
                'D':500,
                'M':1000}
        res = 0
        s = list(s)
        for i in range(len(s)):
            if res==0:
                res+=roman[s[i]]
                continue
            if roman[s[i]]>roman[s[i-1]]:
                res = res + roman[s[i]] - 2*roman[s[i-1]]
                continue
            res+=roman[s[i]]
        return res
    
    

Post a Comment

0 Comments