add leetcode 13
This commit is contained in:
parent
27af23f4d6
commit
d872733763
18
leetcode/13.py
Normal file
18
leetcode/13.py
Normal 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
|
Loading…
x
Reference in New Issue
Block a user