Python Solution (32ms)


  • 0
    G

    class Solution(object):
    def romanToInt(self, s):
    """
    :type s: str
    :rtype: int
    """
    d={'I':1,'V':5,'X':10,'L':50,'C':100,'D':500,'M':1000}
    e={'IV':4,'IX':9,'XL':40,'XC':90,'CD':400,'CM':900}
    num=0
    i=0
    k=0
    while i<len(s):
    try:
    k=s[i]+s[i+1]
    except:
    ValueError
    k=0
    if k in e:
    num+=e[k]
    i+=2
    else:
    num+=d[s[i]]
    i+=1
    return(num)


Log in to reply
 

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