Last active
February 16, 2020 10:19
-
-
Save minhntm/36eea55fac5c393a99e4fb10dfeaba7e to your computer and use it in GitHub Desktop.
Convert Roman number to Integer number
This file contains hidden or 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
class Solution: | |
def romanToInt(self, s: str) -> int: | |
cv_map = { | |
'I': 1, | |
'V': 5, | |
'X': 10, | |
'L': 50, | |
'C': 100, | |
'D': 500, | |
'M': 1000 | |
} | |
result = 0 | |
i = 0 | |
while i < len(s) - 1: | |
if cv_map[s[i]] >= cv_map[s[i+1]]: | |
result = result + cv_map[s[i]] | |
i = i + 1 | |
else: | |
result = result + cv_map[s[i+1]] - cv_map[s[i]] | |
i = i + 2 | |
if i == len(s) - 1: | |
result = result + cv_map[s[i]] | |
return result |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment