-
Notifications
You must be signed in to change notification settings - Fork 0
/
solution13.go
54 lines (51 loc) · 899 Bytes
/
solution13.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
package solution13
// ============================================================================
// 13. Roman to Integer
// URL: https://leetcode.com/problems/roman-to-integer/
// ============================================================================
var m = map[byte]int{
'I': 1,
'V': 5,
'X': 10,
'L': 50,
'C': 100,
'D': 500,
'M': 1000,
}
func romanToInt(s string) int {
length := len(s)
if length < 1 {
return 0
}
var a, b byte
var sum int
for i := 0; i < length; i++ {
a = s[i]
if i < length-1 {
b = s[i+1]
}
switch {
case a == 'I' && b == 'V':
sum += 4
i++
case a == 'I' && b == 'X':
sum += 9
i++
case a == 'X' && b == 'L':
sum += 40
i++
case a == 'X' && b == 'C':
sum += 90
i++
case a == 'C' && b == 'D':
sum += 400
i++
case a == 'C' && b == 'M':
sum += 900
i++
default:
sum += m[a]
}
}
return sum
}