Python code


  • 0
    D
    class Solution(object):
        def romanToInt(self, s):
            """
            :type s: str
            :rtype: int
            """
            v=1000
            value=0
            s1=[['M',1000],['D',500],['C',100],['L',50],['X',10],['V',5],['I',1]]
            l=len(s)
            for i in range(l):
                for j in range(len(s1)):
                    if s[i]==s1[j][0]:
                        if v<s1[j][1]:
                            value=value+s1[j][1]-2*v
                        else:
                            value=value+s1[j][1]
                        v=s1[j][1]
            return value  ```

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.