luoheng
10/11/2019 - 1:59 PM

romanToInt

func romanToInt(s string) int {
    v := map[byte]int{
        'I': 1,
        'V': 5,
        'X': 10,
        'L': 50,
        'C': 100,
        'D': 500,
        'M': 1000,
    }
    sum := 0
    for i := 0; i < len(s) - 1; i++ {
        if v[s[i]] < v[s[i+1]] {
            sum -= v[s[i]]
        } else {
            sum += v[s[i]]
        }
    }
    sum += v[s[len(s)-1]]
    return sum
}