Skip to content

Commit 106015d

Browse files
feat: roman to int
1 parent 1e20d86 commit 106015d

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

resolved/roman-to-int.go

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package resolved
2+
3+
func RomanToInt(s string) int {
4+
hashtable := map[rune]int{
5+
'I': 1,
6+
'V': 5,
7+
'X': 10,
8+
'L': 50,
9+
'C': 100,
10+
'D': 500,
11+
'M': 1000,
12+
}
13+
sum := 0
14+
15+
for idx, romanValue := range s {
16+
if idx != len(s)-1 {
17+
if hashtable[romanValue] < hashtable[rune(s[idx+1])] {
18+
sum -= hashtable[romanValue]
19+
} else {
20+
sum += hashtable[romanValue]
21+
}
22+
} else {
23+
sum += hashtable[romanValue]
24+
}
25+
}
26+
return sum
27+
}

0 commit comments

Comments
 (0)