-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9520a56
commit ce99490
Showing
6 changed files
with
122 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
linters: | ||
enable: | ||
- errorlint | ||
#- godot | ||
- goimports | ||
- misspell | ||
- nolintlint | ||
- predeclared | ||
- testifylint | ||
- unconvert | ||
- durationcheck | ||
- exportloopref | ||
- nilerr | ||
- whitespace | ||
- unparam | ||
- tagalign | ||
- stylecheck | ||
- gosec | ||
#- gofumpt | ||
#- perfsprint | ||
# - nestif | ||
# - noctx | ||
# - prealloc | ||
# - depguard | ||
# - revive | ||
# - gocritic | ||
# - wastedassign | ||
# - goconst | ||
# - gomnd |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/* | ||
13. Roman to Integer | ||
*/ | ||
|
||
package leetcode | ||
|
||
// Time complexity: O(n) | ||
// Space complexity: O(1) | ||
func romanToInteger(s string) int { | ||
roman := map[byte]int{ | ||
'I': 1, | ||
'V': 5, | ||
'X': 10, | ||
'L': 50, | ||
'C': 100, | ||
'D': 500, | ||
'M': 1000, | ||
} | ||
n := len(s) | ||
total := 0 | ||
|
||
for i := 0; i < n; i++ { | ||
if i+1 < n && roman[s[i]] < roman[s[i+1]] { | ||
total -= roman[s[i]] | ||
} else { | ||
total += roman[s[i]] | ||
} | ||
} | ||
|
||
return total | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package leetcode | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
) | ||
|
||
func TestRomanToInteger(t *testing.T) { | ||
var tests = []struct { | ||
name string | ||
input string | ||
output int | ||
}{ | ||
{"case1", "III", 3}, | ||
{"case2", "LVIII", 58}, | ||
{"case3", "MCMXCIV", 1994}, | ||
{"case4", "CMXCVIII", 998}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
result := romanToInteger(tt.input) | ||
if !reflect.DeepEqual(result, tt.output) { | ||
t.Errorf("romanToInteger(%v) = %v; want %v", tt.input, result, tt.output) | ||
} | ||
}) | ||
} | ||
} |