Given a roman numeral, convert it to an integer.
Input is guaranteed to be within the range from 1 to 3999.
func romanToInt(s string) int {
var num int
for i:= 0;i <len(s)-1; i++{
//如果字符与之后的对应数值小,则本字符对应数值为减,否则为加
if getRomanValue(s[i]) < getRomanValue(s[i+1]){
num -= getRomanValue(s[i]);
}else{
num += getRomanValue(s[i]);
}
}
//加上最后一个字符
num += getRomanValue(s[len(s) - 1]);
return num;
}
func getRomanValue( c byte) 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
default: return 0
}
}