/
13.go
65 lines (55 loc) · 1.09 KB
/
13.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
ipackage main
import (
"fmt"
)
/*
13. 罗马数字转整数
给定一个罗马数字,将其转换成整数。
返回的结果要求在 1 到 3999 的范围内。
思路: 具体解法请参考 https://baike.baidu.com/item/%E7%BD%97%E9%A9%AC%E6%95%B0%E5%AD%97/772296?fr=aladdin
*/
func main() {
romanToInt := romanToInt("MCMXCVI")
fmt.Println("romanToInt:", romanToInt)
}
func romanToInt(s string) int {
intNum := 0
var lastChar byte
for index, char := range s {
tempNum := 0
if index > 0 {
lastChar = s[index-1]
}
switch char {
case 'I':
tempNum = 1
case 'V':
tempNum = 5
case 'X':
tempNum = 10
case 'L':
tempNum = 50
case 'C':
tempNum = 100
case 'D':
tempNum = 500
case 'M':
tempNum = 1000
}
tempLastNum := 0
switch lastChar {
case 'I':
tempLastNum = 1
case 'X':
tempLastNum = 10
case 'C':
tempLastNum = 100
}
if (lastChar == 'I' || lastChar == 'X' || lastChar == 'C') && tempLastNum > 0 && tempLastNum < tempNum {
intNum = tempNum + intNum - tempLastNum*2
} else {
intNum = tempNum + intNum
}
}
return intNum
}