package main
/*
* @lc app=leetcode.cn id=13 lang=golang
*
* [13] 罗马数字转整数
2. I X C 也是写成从大到小
3. 左高位,右低位
4. 输入: s = "MC MX CI V"
1100 1010 101 5
输出: 1994
解释: M = 1000, CM = 900, XC = 90, IV = 4.
MMMCCCXXXIII
1000+1000+1000 -100-100-100-10-10-10+1+1+1
CXCIX
100 -10 +100-1+10
*/
// @lc code=start
func romanToInt(s string) int {
var intSum int
for i := 0; i < len(s)-1; i++ {
if char2Int(string(s[i])) > char2Int(string(s[i+1])) {
intSum = intSum + char2Int(string(s[i]))
} else if char2Int(string(s[i])) < char2Int(string(s[i+1])) {
intSum = intSum - char2Int(string(s[i]))
} else {
intSum = intSum + char2Int(string(s[i]))
}
}
intSum = intSum + char2Int(string(s[len(s)-1]))
return intSum
}
func char2Int(c string) int {
switch c {
case "I":
return 1
case "V":
return 5
case "X":
return 10
case "L":
return 50
case "C":
return 100
case "D":
return 500
case "M":
return 1000
}
panic("excep I V X L C D M")
}
// @lc code=end