Share my python code


  • 0
    E
    class Solution:
        # @return an integer
        def romanToInt(self, s):
            a = dict(NUL=0, I=1, V=5, X=10, L=50, C=100, D=500, M=1000)
            int = 0
            pre = 'NUL'
            s = s[::-1]
            for l in s:
                now = l
                if a[now] >= a[pre]:
                    int = int + a[now]
                elif a[now] < a[pre]:
                    int = int - a[now]
                pre = l
            return int
    

    Reserve the Roman Number and find out the inverted one in it.


Log in to reply
 

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