Skip to content

Instantly share code, notes, and snippets.

@BharathKumarS
Created February 18, 2020 14:02
Show Gist options
  • Save BharathKumarS/f98c9186b0fade85e4b3d104f286d0a9 to your computer and use it in GitHub Desktop.
Save BharathKumarS/f98c9186b0fade85e4b3d104f286d0a9 to your computer and use it in GitHub Desktop.
Roman number to Integer. Very efficient with Time and Space complexity
#This solution works only when executed on LeetCode. For test cases and practice, check my git repo
class Solution:
def romanToInt(self, s: str) -> int:
Guide = {'I':1, 'V':5, 'X':10, 'L':50, 'C':100, 'D': 500, 'M':1000}
num = prev_char = 0
for char in s[::-1]:
if Guide[char] >= prev_char:
prev_char = Guide[char]
num = num + Guide[char]
else:
num = num - Guide[char]
return(num)
@BharathKumarS
Copy link
Author

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment