Skip to content

Commit

Permalink
Add 2Sum
Browse files Browse the repository at this point in the history
  • Loading branch information
LuaanNguyen committed Jan 6, 2025
1 parent 20fc978 commit b2701db
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 7 deletions.
20 changes: 13 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,34 @@ This repository contains my solutions to LeetCode problems in [Go](https://go.de

<img src="https://raw.githubusercontent.com/gist/brudnak/6c21505423e4ff089ab704ec79b5a096/raw/b2d3dec32474b2121b179920734b259323a7c250/go.gif" alt="Go" width="200"/>

## Folder Structure 🗂️
### Folder Structure 🗂️

- `easy/`: Easy level problems.
- `medium/`: Medium level problems.
- `hard/`: Hard level problems.

<!-- ## Example Usage (To be added)
### Unit Testing 🧪

You can clone this repository and run individual solutions using the Go command:
In your root direction, run:

```bash
go run .
``` -->
go test ./...
```

If you only want to run tests in a specific problem `(e.g., ./easy/0001_two_sum)`, run:

```bash
go test ./easy/0001_two_sum
```

## Solutions (Continue Updating...)
### Solutions (Continue Updating...)

| Leetcode ID | Title & Solution | Coefficient Of Difficulty | Remarks |
| :---------------------------------------------------------------: | :----------------------- | :-----------------------: | :--------------: |
| [0001](https://leetcode.com/problems/two-sum/description/) | [Two Sum](/easy/) | Easy | _`HashMap`_ |
| [0392](https://leetcode.com/problems/is-subsequence/description/) | [Is Subsequence](/easy/) | Medium | _`Two Pointers`_ |

## License 🪪
### License 🪪

```txt
MIT License
Expand Down
12 changes: 12 additions & 0 deletions easy/0001_two_sum/0001_two_sum.go
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
}
34 changes: 34 additions & 0 deletions easy/0001_two_sum/0001_two_sum_test.go
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)
}
}
})
}
}
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module github.com/LuaanNguyen/go-leetcode

go 1.23.0

0 comments on commit b2701db

Please sign in to comment.