Created
July 28, 2018 10:57
-
-
Save luojiyin1987/20b9c2a477f40a581a28749043e42400 to your computer and use it in GitHub Desktop.
Roman to Integer
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 { | |
| 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