Skip to content

Instantly share code, notes, and snippets.

@aegypius
Last active May 24, 2023 15:29
Show Gist options
  • Save aegypius/9ba8953b134e0b222828340b678dfbe9 to your computer and use it in GitHub Desktop.
Save aegypius/9ba8953b134e0b222828340b678dfbe9 to your computer and use it in GitHub Desktop.
Roman To Number
func romanToInt(s string) int {
var result int
var last byte
romanToIntMap := map[byte]int{
'I': 1,
'V': 5,
'X': 10,
'L': 50,
'C': 100,
'D': 500,
'M': 1000,
}
for i := len(s) - 1; i >= 0; i-- {
current := romanToIntMap[s[i]]
if current >= romanToIntMap[last] {
result += current
} else {
result -= current
}
last = s[i]
}
return result
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment