-
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
20fc978
commit b2701db
Showing
4 changed files
with
62 additions
and
7 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
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,12 @@ | ||
package two_sum | ||
|
||
func twoSum(nums []int, target int) []int { | ||
m := make(map[int]int) | ||
for i, num := range nums { | ||
if j, found := m[target-num]; found { | ||
return []int{j, i} | ||
} | ||
m[num] = i | ||
} | ||
return nil | ||
} |
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,34 @@ | ||
|
||
package two_sum | ||
|
||
import "testing" | ||
|
||
func TestTwoSum(t *testing.T) { | ||
// Define test cases | ||
tests := []struct { | ||
nums []int | ||
target int | ||
result []int | ||
}{ | ||
{[]int{2, 7, 11, 15}, 9, []int{0, 1}}, | ||
{[]int{3, 2, 4}, 6, []int{1, 2}}, | ||
{[]int{3, 3}, 6, []int{0, 1}}, | ||
} | ||
|
||
// Loop through test cases | ||
for _, test := range tests { | ||
t.Run("Test", func(t *testing.T) { | ||
got := twoSum(test.nums, test.target) | ||
if len(got) != len(test.result) { | ||
t.Errorf("Expected result %v, but got %v", test.result, got) | ||
} | ||
|
||
// Compare the results | ||
for i := range got { | ||
if got[i] != test.result[i] { | ||
t.Errorf("For input %v with target %d, expected result %v, but got %v", test.nums, test.target, test.result, got) | ||
} | ||
} | ||
}) | ||
} | ||
} |
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,3 @@ | ||
module github.com/LuaanNguyen/go-leetcode | ||
|
||
go 1.23.0 |