Skip to content

Instantly share code, notes, and snippets.

@dabsclement
Created December 24, 2020 06:43
Show Gist options
  • Save dabsclement/c2ccf2caf2dd689fd88d8e614b7cb3ef to your computer and use it in GitHub Desktop.
Save dabsclement/c2ccf2caf2dd689fd88d8e614b7cb3ef to your computer and use it in GitHub Desktop.
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