This commit is contained in:
2026-07-02 16:37:27 +08:00
parent 8f176c1836
commit 4850991edd
2 changed files with 78 additions and 0 deletions
+34
View File
@@ -0,0 +1,34 @@
package top100liked
import (
"fmt"
"testing"
)
// https://leetcode.cn/problems/subsets/?envType=study-plan-v2&envId=top-100-liked
func subsets(nums []int) [][]int {
res := [][]int{{}}
var helper func(nums []int, length int, start int, r []int)
helper = func(nums []int, length int, start int, r []int) {
if len(r) == length {
cp := make([]int, length)
copy(cp, r)
res = append(res, cp)
return
}
for i := start; i < len(nums); i++ {
r = append(r, nums[i])
helper(nums, length, i+1, r)
r = r[:len(r)-1]
}
}
for i := range len(nums) {
helper(nums, i+1, 0, []int{})
}
return res
}
func Test(t *testing.T) {
fmt.Printf("%+v", subsets([]int{1, 2, 3}))
}