every blog every motto: You will never know unless you try
0. 前言
冲冲冲
1. 字符串
1.1 题目
1.2 代码
class Solution:
def romanToInt(self, s: str) -> int:
pattern = {
'IV': 4, 'IX': 9,
'XL': 40, 'XC': 90,
'CD': 400, 'CM': 900
}
sv = {
'I': 1, 'V': 5, 'X': 10, 'L': 50, 'C': 100, 'D': 500, 'M': 1000
}
if s in pattern:
return pattern[s]
index = 0
result = 0
while index < len(s) :
if s[index:index + 2] in pattern:
dig = pattern[s[index:index + 2]]
index += 2
else:
dig = sv[s[index]]
index += 1
result +=dig
return result
1.3 结果
|