From 8af2fdb9ab53dea9a65d1ee6183032536b4c587f Mon Sep 17 00:00:00 2001 From: prackode <72190853+prackode@users.noreply.github.com> Date: Tue, 6 Oct 2020 00:02:23 +0530 Subject: [PATCH] Create 0013_Roman_to_Integer.py --- LeetCode/0013_Roman_to_Integer.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 LeetCode/0013_Roman_to_Integer.py 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