u
This commit is contained in:
@@ -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}))
|
||||
}
|
||||
Reference in New Issue
Block a user