Python solution


  • 0
    A

    class Solution:
    def romanToInt(self, s):
    x = {"I": 1, "V": 5, "X": 10, "L": 50, "C":100, "D": 500, "M": 1000}
    dec = 0
    for i in xrange(len(s)):
    if i > 0 and x[s[i]] > x[s[i - 1]]:
    dec += x[s[i]] - 2 * x[s[i - 1]]
    else:
    dec += x[s[i]]
    return dec

    if name == "main":
    print Solution().romanToInt("IIVX")
    print Solution().romanToInt("MMMCMXCIX")


Log in to reply
 

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