Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 to 3999.
翻译
给出一个罗马数字,将其转化为整数。 输入被限定在在1到3999之间。
func romanToInt(s string) int {
var result int
for len(s) > 0 {
if len(s) >= 2 {
curr := findRoman(s[:2])
if curr != 0 {
result += curr
s = s[2:]
continue
}
}
result += findRoman(s[:1])
s = s[1:]
}
return result
}
func findRoman(s string) int {
switch s {
case "I":
return 1
case "IV":
return 4
case "V":
return 5
case "IX":
return 9
case "X":
return 10
case "XL":
return 40
case "L":
return 50
case "XC":
return 90
case "C":
return 100
case "CD":
return 400
case "D":
return 500
case "CM":
return 900
case "M":
return 1000
default:
return 0
}
}
暂时没有留言