Skip to content

Commit

Permalink
feat: add romanToInteger solution
Browse files Browse the repository at this point in the history
  • Loading branch information
angelicagardner committed Jun 17, 2024
1 parent 9520a56 commit ce99490
Show file tree
Hide file tree
Showing 6 changed files with 122 additions and 17 deletions.
29 changes: 29 additions & 0 deletions .golangci.yaml
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
4 changes: 2 additions & 2 deletions LeetCode/problem001_TwoSum.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ package leetcode

// Time complexity: O(n^2)
// Space complexity: O(1)
func TwoSumBruteForce(nums []int, target int) []int {
func twoSumBruteForce(nums []int, target int) []int {
for i := 0; i < len(nums); i++ {
for j := i + 1; j < len(nums); j++ {
if nums[i]+nums[j] == target {
Expand All @@ -19,7 +19,7 @@ func TwoSumBruteForce(nums []int, target int) []int {

// Time complexity: O(n)
// Space complexity: O(n)
func TwoSumHashMap(nums []int, target int) []int {
func twoSumHashMap(nums []int, target int) []int {
hashMap := make(map[int]int)
for i, num := range nums {
complement := target - num
Expand Down
40 changes: 26 additions & 14 deletions LeetCode/problem001_TwoSum_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,22 @@ import (
"testing"
)

var tests = []struct {
name string
nums []int
target int
output []int
}{
{"case1", []int{2, 7, 11, 15}, 9, []int{0, 1}},
{"case2", []int{2, 1, 5, 3}, 4, []int{1, 3}},
{"case3", []int{3, 2, 4}, 6, []int{1, 2}},
{"case4", []int{3, 3}, 6, []int{0, 1}},
}

func TestTwoSumBruteForce(t *testing.T) {
var tests = []struct {
name string
nums []int
target int
output []int
}{
{"case1", []int{2, 7, 11, 15}, 9, []int{0, 1}},
{"case2", []int{2, 1, 5, 3}, 4, []int{1, 3}},
{"case3", []int{3, 2, 4}, 6, []int{1, 2}},
{"case4", []int{3, 3}, 6, []int{0, 1}},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := TwoSumBruteForce(tt.nums, tt.target)
result := twoSumBruteForce(tt.nums, tt.target)
if !reflect.DeepEqual(result, tt.output) {
t.Errorf("TwoSumBruteForce(%v, %v) = %v; want %v", tt.nums, tt.target, result, tt.output)
}
Expand All @@ -29,9 +29,21 @@ func TestTwoSumBruteForce(t *testing.T) {
}

func TestTwoSumHashMap(t *testing.T) {
var tests = []struct {
name string
nums []int
target int
output []int
}{
{"case1", []int{2, 7, 11, 15}, 9, []int{0, 1}},
{"case2", []int{2, 1, 5, 3}, 4, []int{1, 3}},
{"case3", []int{3, 2, 4}, 6, []int{1, 2}},
{"case4", []int{3, 3}, 6, []int{0, 1}},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := TwoSumHashMap(tt.nums, tt.target)
result := twoSumHashMap(tt.nums, tt.target)
if !reflect.DeepEqual(result, tt.output) {
t.Errorf("TwoSumHashMap(%v, %v) = %v; want %v", tt.nums, tt.target, result, tt.output)
}
Expand Down
7 changes: 6 additions & 1 deletion LeetCode/problem009_PalindromeNumber.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
/* Problem 9. Palindrome Number */
/*
Problem 9. Palindrome Number
*/

package leetcode

import (
"math"
)

// Time complexity: O(log10​(n))
// Space complexity: O(1)
func isPalindrome(x int) bool {
// Negative numbers are not palindromes
if x < 0 {
Expand Down
31 changes: 31 additions & 0 deletions LeetCode/problem013_RomanToInteger.go
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
}
28 changes: 28 additions & 0 deletions LeetCode/problem013_RomanToInteger_test.go
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)
}
})
}
}

0 comments on commit ce99490

Please sign in to comment.