Created
December 24, 2020 06:43
-
-
Save dabsclement/c2ccf2caf2dd689fd88d8e614b7cb3ef to your computer and use it in GitHub Desktop.
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
func romanToInt(s string) int { | |
romanNums := map[byte]int{ | |
'I': 1, | |
'V': 5, | |
'X': 10, | |
'L': 50, | |
'C': 100, | |
'D': 500, | |
'M': 1000, | |
} | |
var sum int | |
for i := len(s) - 1; i >= 0; i-- { | |
current := romanNums[s[i]] | |
if i > 0 && romanNums[s[i-1]] < current { | |
current -= romanNums[s[i-1]] | |
i-- | |
} | |
sum += current | |
} | |
return sum | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment