MCPcopy Create free account
hub / github.com/austingebauer/go-leetcode / romanToInt

Function romanToInt

roman_to_integer_13/solution.go:3–54  ·  view source on GitHub ↗
(s string)

Source from the content-addressed store, hash-verified

1package roman_to_integer_13
2
3func romanToInt(s string) int {
4 out := 0
5 numMap := map[string]int{
6 "I": 1,
7 "V": 5,
8 "X": 10,
9 "L": 50,
10 "C": 100,
11 "D": 500,
12 "M": 1000,
13 }
14
15 cIdx := 0
16 for cIdx < len(s) {
17 c := string(s[cIdx])
18 if c == "I" {
19 if cIdx < len(s)-1 {
20 next := string(s[cIdx+1])
21 if next == "V" || next == "X" {
22 out += numMap[next] - numMap[c]
23 cIdx += 2
24 continue
25 }
26 }
27 }
28 if c == "X" {
29 if cIdx < len(s)-1 {
30 next := string(s[cIdx+1])
31 if next == "L" || next == "C" {
32 out += numMap[next] - numMap[c]
33 cIdx += 2
34 continue
35 }
36 }
37 }
38 if c == "C" {
39 if cIdx < len(s)-1 {
40 next := string(s[cIdx+1])
41 if next == "D" || next == "M" {
42 out += numMap[next] - numMap[c]
43 cIdx += 2
44 continue
45 }
46 }
47 }
48
49 out += numMap[c]
50 cIdx++
51 }
52
53 return out
54}

Callers 1

Test_romanToIntFunction · 0.85

Calls

no outgoing calls

Tested by 1

Test_romanToIntFunction · 0.68