Skip to content

Commit 980850c

Browse files
authored
Create Roman to integer - Leetcode 13.go
go solution to Roman to integer
1 parent ee6f841 commit 980850c

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package solution
2+
3+
func romanToInt(s string) int {
4+
val := 0
5+
prevVal := 1
6+
for i := len(s) - 1; i >= 0; i-- {
7+
sVal := symbolVal(s[i])
8+
if prevVal > sVal {
9+
val -= sVal
10+
} else {
11+
val += sVal
12+
}
13+
prevVal = sVal
14+
}
15+
return val
16+
}
17+
18+
func symbolVal(symbol byte) int {
19+
switch symbol {
20+
case 'M':
21+
return 1000
22+
case 'D':
23+
return 500
24+
case 'C':
25+
return 100
26+
case 'L':
27+
return 50
28+
case 'X':
29+
return 10
30+
case 'V':
31+
return 5
32+
default:
33+
return 1
34+
}
35+
}

0 commit comments

Comments
 (0)