add leetcode 13

This commit is contained in:
sangge-rockpi 2024-01-19 23:05:45 +08:00
parent 27af23f4d6
commit d872733763

18
leetcode/13.py Normal file
View File

@ -0,0 +1,18 @@
# translate roman number strings to int
class Solution:
def remanToInt(self, s: str) -> int:
roman_map = {"I": 1, "V": 5, "X": 10, "L": 50, "C": 100, "D": 500, "M": 1000}
num = 0
prev_value = 0
for char in reversed(s):
value = roman_map[char]
if value < prev_value:
num -= value
else:
num += value
prev_value = value
return num