Last active
May 24, 2023 15:29
-
-
Save aegypius/9ba8953b134e0b222828340b678dfbe9 to your computer and use it in GitHub Desktop.
Roman To Number
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
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