Created
February 18, 2020 14:02
-
-
Save BharathKumarS/f98c9186b0fade85e4b3d104f286d0a9 to your computer and use it in GitHub Desktop.
Roman number to Integer. Very efficient with Time and Space complexity
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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) |
Author
BharathKumarS
commented
Feb 18, 2020
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment