Share python code

  • 6

    The main problem is to deal with the special numbers "IV","IX","XL","XC","CD","CM",for in these numbers "I","X","C" have the unusual meaning(subtract not plus).

    If given a Roman without numbers mentioned above,we can easily get the result by plusing all the numbers from right to left .

    So we need to find out what "I","X","C" mean when they appear(mean plus or subtract).

    "I" for example,the symbol "I" only appear in following circumstances:
    "I","II","III","VI",VII","VIII","IV","IX","XI"····(omitting "IIII" for it is special) .

    We can figure out that when we plus the symbols from left to right. If sum>=5,then the "I" we get can only mean "-1".Otherwise,it means "+1".The same is true with "X" and "C".


    class Solution:
        # @return an integer
        def romanToInt(self, s):
            for i in s[-1::-1]:
                if (i in ['I','X','C']) and result>=5*dic[i]:
            return result

Log in to reply

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