Skip to content

Commit 5d498f3

Browse files
author
Yeqi Tao
committed
Add Soulution.go for 0013.Roman to Integer
1 parent 02ba3d1 commit 5d498f3

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed
+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// 字符 数值
2+
// I 1
3+
// V 5
4+
// X 10
5+
// L 50
6+
// C 100
7+
// D 500
8+
// M 1000
9+
var table = make(map[string]int)
10+
11+
func init() {
12+
table["I"] = 1
13+
table["IV"] = 4
14+
table["V"] = 5
15+
table["IX"] = 9
16+
table["X"] = 10
17+
table["XL"] = 40
18+
table["L"] = 50
19+
table["XC"] = 90
20+
table["C"] = 100
21+
table["CD"] = 400
22+
table["D"] = 500
23+
table["CM"] = 900
24+
table["M"] = 1000
25+
}
26+
27+
func romanToInt(s string) int {
28+
var result int
29+
lenS := len(s)
30+
for i:=0; i<lenS; i++ {
31+
if i < lenS - 1 {
32+
if v, exist := table[s[i:i+2]]; exist {
33+
result += v
34+
i++
35+
continue
36+
}
37+
}
38+
result += table[string(s[i])]
39+
}
40+
return result
41+
}

0 commit comments

Comments
 (0)