Why my python's code can't pass the test case "MCDLXXVL"? while it 's right to others


  • -1

    Why my python's code can't pass the test case "MCDLXXVL"? while it 's right to others
    class Solution(object):
    def romanToInt(self, s):
    """
    :type s: str
    :rtype: int
    """
    Num = {"I":1, "V":5, "X":10, "L":50, "C":100, "D":500, "M":1000}
    L = len(s)
    sum = 0
    i = L - 1
    while(i >= 0):

            if(i - 1 >= 0):
                if(Num.get(s[i - 1]) == Num.get(s[i])):  # XX=20
                    sum += Num.get(s[i]) * 2
                    
                elif(Num.get(s[i - 1]) < Num.get(s[i])):  # IV=4
                    sum += Num.get(s[i]) - Num.get(s[i - 1])
                    
                elif(Num.get(s[i - 1]) > Num.get(s[i])):
                    sum += Num.get(s[i - 1]) + Num.get(s[i])  # VI=6
                    
                i -= 2
            else:
                sum += Num.get(s[i])
                i -= 1
                
        return sum

  • 0
    C
    This post is deleted!

Log in to reply
 

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