A pythonic way of doing it with explanation.

  • 0

    Roman Integers are represented as I-1;V-5;X-10;L-50;C-100;D-500;M-1000
    Generally Roman numbers are added from left to right to get its equivalent integer.
    However in certain cases, if the preceding letter is lesser than the current letter, the two numbers are
    subtracted. Eg:
    399 = CCCXCIX

        res = 0
        i = 0
        while i < len(s):
            if i+1<len(s) and dict[s[i]] < dict[s[i + 1]]:
                res += dict[s[i + 1]] - dict[s[i]]
                i+= 2
                res += dict[s[i]]
                i += 1
        return res

  • 0

    Hi, This code can be accepted, but if you use case 'LCDM', it will get the wrong answer

Log in to reply

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