Skip to content

Commit 6c0119a

Browse files
authored
refactor: move LeetCode solutions to per-problem package directories (#3)
* refactor: move each LeetCode solution into per-problem package dirs * chore: align docs, validation, and CI with per-problem layout * fix: use keyed struct literals for util types * fix: key TreeNode literals in tests for govet
1 parent b3971aa commit 6c0119a

File tree

721 files changed

+2938
-3464
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

721 files changed

+2938
-3464
lines changed

.github/workflows/ci.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ jobs:
2323
- name: Setup Go
2424
uses: actions/setup-go@v5
2525
with:
26-
go-version-file: localhost/leetcode/go.mod
26+
go-version: '1.26.x'
2727
cache-dependency-path: |
2828
localhost/leetcode/go.sum
2929
@@ -42,7 +42,7 @@ jobs:
4242
- name: Setup Go
4343
uses: actions/setup-go@v5
4444
with:
45-
go-version-file: localhost/leetcode/go.mod
45+
go-version: '1.26.x'
4646
cache-dependency-path: |
4747
localhost/leetcode/go.sum
4848

README.md

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,27 +6,26 @@ Repository for practicing LeetCode problems in Go with standardized structure, C
66

77
- `localhost/leetcode/`: Go module root.
88
- `localhost/leetcode/problems/<0001-0300>/`: range folders (300 problems each).
9-
- `localhost/leetcode/problems/<range>/solution_<id>.go`: solution implementation.
10-
- `localhost/leetcode/problems/<range>/solution_<id>_test.go`: solution tests.
11-
- `localhost/leetcode/constants.go`: canonical shared constants file.
12-
- `localhost/leetcode/leetcode_shared_libs.go`: canonical shared helper functions.
13-
- `localhost/leetcode/problems/<range>/constants.go`: symlink to `../../constants.go`.
14-
- `localhost/leetcode/problems/<range>/leetcode_shared_libs.go`: symlink to `../../leetcode_shared_libs.go`.
15-
- `localhost/leetcode/problems/<range>/types.go`: range-local compatibility types.
9+
- `localhost/leetcode/problems/<range>/p<id>/solution_<id>.go`: solution implementation.
10+
- `localhost/leetcode/problems/<range>/p<id>/solution_<id>_test.go`: solution tests.
11+
- `localhost/leetcode/util/constants.go`: shared constants.
12+
- `localhost/leetcode/util/helpers.go`: shared helper functions.
13+
- `localhost/leetcode/util/types.go`: shared types.
1614
- `.github/workflows/ci.yml`: CI pipeline.
1715
- `scripts/new_solution.sh`: Creates standardized solution stubs.
18-
- `scripts/validate_solutions.sh`: Validates layout, naming, package conventions, and shared-file symlinks.
16+
- `scripts/validate_solutions.sh`: Validates per-problem layout and package conventions.
1917
- `.pre-commit-config.yaml`: Local pre-commit checks.
2018

2119
## Conventions
2220

2321
- Go version: `1.26` (`go.mod` + `go.work`).
24-
- Each solution lives in a range folder:
25-
- `localhost/leetcode/problems/<range>/solution_<problem_id>.go`
26-
- Each test lives alongside the solution:
27-
- `localhost/leetcode/problems/<range>/solution_<problem_id>_test.go`
28-
- File package must be `solutions`.
29-
- Shared constants/helpers are single-source at module root and symlinked into each range folder.
22+
- Each solution lives in:
23+
- `localhost/leetcode/problems/<range>/p<problem_id>/solution_<problem_id>.go`
24+
- Each test lives alongside the solution in the same `p<problem_id>` directory.
25+
- Package name must match the problem directory:
26+
- directory `p382` uses `package p382`
27+
- Shared helpers/types/constants live under:
28+
- `localhost/leetcode/util`
3029

3130
## Local Commands
3231

@@ -72,6 +71,6 @@ For this repo, "CD" is continuous delivery of quality gates to `main` (no deploy
7271

7372
## Workflow Notes
7473

75-
- CI uses Go from `localhost/leetcode/go.mod` (currently `1.26`).
74+
- CI pins Go to `1.26.x` in `.github/workflows/ci.yml`.
7675
- Lint workflow uses `golangci-lint-action@v9` with `golangci-lint v2`.
77-
- `scripts/validate_solutions.sh` supports both `rg` and `grep` so it works on runners without `ripgrep`.
76+
- `scripts/validate_solutions.sh` supports both `rg` and `grep`, so it works on runners without `ripgrep`.

localhost/leetcode/problems/0001-0300/constants.go

Lines changed: 0 additions & 1 deletion
This file was deleted.

localhost/leetcode/problems/0001-0300/leetcode_shared_libs.go

Lines changed: 0 additions & 1 deletion
This file was deleted.
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package p1
2+
3+
// twoSum_1 returns indices of the two numbers such that they add up to target.
4+
func twoSum_1(nums []int, target int) []int {
5+
result := make([]int, 0, 2)
6+
seenByValue := make(map[int]int, len(nums))
7+
8+
for i, num := range nums {
9+
if j, ok := seenByValue[target-num]; ok {
10+
result = append(result, j, i)
11+
}
12+
seenByValue[num] = i
13+
}
14+
15+
return result
16+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package p1
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
)
8+
9+
func TestSolution1(t *testing.T) {
10+
assert.Equal(t, []int{0, 1}, twoSum_1([]int{2, 7, 11, 15}, 9), "Solution1")
11+
}

localhost/leetcode/problems/0001-0300/solution_100.go renamed to localhost/leetcode/problems/0001-0300/p100/solution_100.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
package solutions
1+
package p100
22

3-
func isSameTree(p *TreeNode, q *TreeNode) bool {
3+
import "localhost/leetcode/util"
4+
5+
func isSameTree(p *util.TreeNode, q *util.TreeNode) bool {
46
if p == nil && q == nil {
57
return true
68
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package p100
2+
3+
import (
4+
"github.com/stretchr/testify/assert"
5+
"localhost/leetcode/util"
6+
"sort"
7+
"strings"
8+
"testing"
9+
)
10+
11+
var (
12+
_ = sort.Strings
13+
_ = strings.Join
14+
)
15+
16+
func TestSolution100(t *testing.T) {
17+
assert := assert.New(t)
18+
19+
root1 := util.TreeNode{Val: 1, Left: nil, Right: nil}
20+
root1.Left = &util.TreeNode{Val: 2, Left: nil, Right: nil}
21+
root1.Right = &util.TreeNode{Val: 3, Left: nil, Right: nil}
22+
23+
root2 := util.TreeNode{Val: 1, Left: nil, Right: nil}
24+
root2.Left = &util.TreeNode{Val: 2, Left: nil, Right: nil}
25+
root2.Right = &util.TreeNode{Val: 3, Left: nil, Right: nil}
26+
27+
actual := isSameTree(&root1, &root2)
28+
29+
assert.True(actual, "Solution100")
30+
31+
}

localhost/leetcode/problems/0001-0300/solution_102.go renamed to localhost/leetcode/problems/0001-0300/p102/solution_102.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1-
package solutions
1+
package p102
22

3-
func levelOrder(root *TreeNode) [][]int {
3+
import "localhost/leetcode/util"
4+
5+
func levelOrder(root *util.TreeNode) [][]int {
46
if root == nil {
57
return [][]int{}
68
}
79

8-
q := make([]*TreeNode, 0)
10+
q := make([]*util.TreeNode, 0)
911
q = append(q, root)
1012

1113
ans := make([][]int, 0)
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package p102
2+
3+
import (
4+
"github.com/stretchr/testify/assert"
5+
"localhost/leetcode/util"
6+
"sort"
7+
"strings"
8+
"testing"
9+
)
10+
11+
var (
12+
_ = sort.Strings
13+
_ = strings.Join
14+
)
15+
16+
func TestSolution102(t *testing.T) {
17+
assert := assert.New(t)
18+
19+
root := util.TreeNode{Val: 6, Left: nil, Right: nil}
20+
root.Left = &util.TreeNode{Val: 2, Left: nil, Right: nil}
21+
root.Left.Left = &util.TreeNode{Val: 0, Left: nil, Right: nil}
22+
root.Left.Right = &util.TreeNode{Val: 4, Left: nil, Right: nil}
23+
root.Left.Right.Left = &util.TreeNode{Val: 3, Left: nil, Right: nil}
24+
root.Left.Right.Right = &util.TreeNode{Val: 5, Left: nil, Right: nil}
25+
root.Right = &util.TreeNode{Val: 8, Left: nil, Right: nil}
26+
root.Right.Left = &util.TreeNode{Val: 7, Left: nil, Right: nil}
27+
root.Right.Right = &util.TreeNode{Val: 9, Left: nil, Right: nil}
28+
29+
actual := levelOrder(&root)
30+
31+
assert.Equal([][]int{{6}, {2, 8}, {0, 4, 7, 9}, {3, 5}}, actual, "Solution102")
32+
}

0 commit comments

Comments
 (0)