Simple python solution


  • 1
    K
        d={'I':1,'X':10,'C':100,'M':1000,'V':5,'L':50,'D':500}
        m=list(s)
        i=0
        l=len(m)
        r=0
        while i<l:
            r+=d[m[l-i-1]]
            i=i+1
            if i==l:
                break
            if d[m[l-i-1]]<d[m[l-i]]:
                r-=d[m[l-i-1]]
                i=i+1
        return r

Log in to reply
 

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