diff --git a/LeetCode/0013_Roman_to_Integer.py b/LeetCode/0013_Roman_to_Integer.py new file mode 100644 index 0000000..0b44895 --- /dev/null +++ b/LeetCode/0013_Roman_to_Integer.py @@ -0,0 +1,12 @@ +class Solution: + def romanToInt(self, s: str) -> int: + value = {'M': 1000,'D': 500, 'C': 100,'L': 50,'X': 10,'V': 5,'I': 1} + p = 0;ans = 0 + n = len(s) + for i in range(n-1, -1, -1): + if value[s[i]] >= p: + ans += value[s[i]] + else: + ans -= value[s[i]] + p = value[s[i]] + return ans