-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathN013RomanToInt.go
More file actions
62 lines (60 loc) · 1.05 KB
/
N013RomanToInt.go
File metadata and controls
62 lines (60 loc) · 1.05 KB
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
package main
type N013RomanToInt struct {
}
func (this *N013RomanToInt) romanToInt(s string) int {
value := 0
length := len(s)
var current byte
var pre byte
for i := length - 1; i >= 0; {
current = s[i]
if i-1 >= 0 {
pre = s[i-1]
} else {
pre = 0
}
if current == 'I' {
value += 1
i--
} else if pre == 'I' && current == 'V' {
value += 4
i -= 2
} else if current == 'V' {
value += 5
i--
} else if pre == 'I' && current == 'X' {
value += 9
i -= 2
} else if current == 'X' {
value += 10
i--
} else if pre == 'X' && current == 'L' {
value += 40
i -= 2
} else if current == 'L' {
value += 50
i--
} else if pre == 'X' && current == 'C' {
value += 90
i -= 2
} else if current == 'C' {
value += 100
i--
} else if pre == 'C' && current == 'D' {
value += 400
i -= 2
} else if current == 'D' {
value += 500
i--
} else if pre == 'C' && current == 'M' {
value += 900
i -= 2
} else if current == 'M' {
value += 1000
i--
} else {
break
}
}
return value
}