Created
June 9, 2022 03:15
-
-
Save mike-pete/329e3c9ac46613078ceae1495b210c20 to your computer and use it in GitHub Desktop.
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
class Solution: | |
def romanToInt(self, s: str) -> int: | |
numerals = { | |
'I': 1, | |
'V': 5, | |
'X': 10, | |
'L': 50, | |
'C': 100, | |
'D': 500, | |
'M': 1000 | |
} | |
total = 0 | |
numeralList = list(s) | |
valueOfPreviousNumeral = 0 | |
for numeral in numeralList: | |
valueOfCurrentNumeral = numerals[numeral] | |
total += valueOfCurrentNumeral | |
if (valueOfPreviousNumeral < valueOfCurrentNumeral): | |
total -= valueOfPreviousNumeral*2 | |
valueOfPreviousNumeral = valueOfCurrentNumeral | |
return total |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment