Skip to content

Instantly share code, notes, and snippets.

@minhntm
Last active February 16, 2020 10:19
Show Gist options
  • Save minhntm/36eea55fac5c393a99e4fb10dfeaba7e to your computer and use it in GitHub Desktop.
Save minhntm/36eea55fac5c393a99e4fb10dfeaba7e to your computer and use it in GitHub Desktop.
Convert Roman number to Integer number
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