Skip to content

Instantly share code, notes, and snippets.

@luojiyin1987
Created July 28, 2018 10:57
Show Gist options
  • Save luojiyin1987/20b9c2a477f40a581a28749043e42400 to your computer and use it in GitHub Desktop.
Save luojiyin1987/20b9c2a477f40a581a28749043e42400 to your computer and use it in GitHub Desktop.
Roman to Integer
func romanToInt(s string) int {
numbers := map[string]int{"M":1000, "D":500, "C":100, "L":50, "X":10, "V":5, "I":1}
last := -1
sum := 0
bytes := []byte(s)
for i := range bytes{
if last > 0 && numbers[string(bytes[i])] > last {
sum -= 2 * last
}
sum += numbers[string(bytes[i])]
last = numbers[string(bytes[i])]
}
return sum
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment