Easy python solution


  • 0
    A
    class Solution(object):
        def romanToInt(self, s):
            p={'M':1000,'D':500,'C':100,'L':50,'X':10,'V':5,'I':1}
            ans=0;
            left=0
            for v in s:
                if left<p[v]:
                    ans=ans-2*left
                ans=ans+p[v]
                left=p[v]
            return ans

  • 0
    X

    it's error,because if the romen is MCMXXC,right answer is 1980,but your is 2000.


  • 0
    A

    Hi, i think "MCMXXC" is not a legal Roman number, even though i use "MCMXXC" for test, the expected answer is also 2000.


  • 0
    X

    thanks,the romen number is error after google,romen number 1980 is MCMLXXX .But I can't find it in Baidu.I'm ture that google is better than Baidu.thanks for your answer!


Log in to reply
 

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