Python2 simple solution by Joey簡單解法


  • 0
    T

    By using same concept as int to Roman, because we read Roman from left to right, so we evaluate it by check if the string is in the index 0. Using find() function is much simple.
    想成使用find()查表,如果最左邊的有roman中最大的字符則將他擷取掉並且加上相對應的十進位數字。

    decimal = [1000,900,500,400,100,90,50,40,10,9,5,4,1]
    roman = ['M','CM','D','CD','C','XC','L','XL','X','IX','V','IV','I']
    ans = 0
    for i in range(len(decimal)):
        while s.find(roman[i])==0:
            s=s[len(roman[i]):]
            ans += decimal[i] 
    return ans
    

Log in to reply
 

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