smilelc3 / leetcode

Leetcode Solution C/C++/Golang Version(leetocde 刷题解答,C、C++、Golang 版本 )
GNU General Public License v3.0
3 stars 2 forks source link

第二题 #35

Closed smilelc3 closed 2 years ago

smilelc3 commented 2 years ago
# coding=utf-8

def roman_to_int(string):
    num = []
    d = {'I': 1, 'V': 5, 'X': 10, 'L': 50, 'C': 100, 'D': 500, 'M': 1000}
    for ch in string:
        num.append(d[ch])
    for i in range(len(num) - 1):
        if num[i] < num[i + 1]:
            num[i] = -num[i]
    return sum(num)

s = input()
print(roman_to_int(s))