mirror of
https://gitlab.com/mfocko/LeetCode.git
synced 2024-11-10 00:09:06 +01:00
go: add «78. Subsets»
Signed-off-by: Matej Focko <me@mfocko.xyz>
This commit is contained in:
parent
928354c370
commit
370c61deb6
1 changed files with 29 additions and 0 deletions
29
go/subsets.go
Normal file
29
go/subsets.go
Normal file
|
@ -0,0 +1,29 @@
|
||||||
|
package subsets
|
||||||
|
|
||||||
|
func subsets(nums []int) [][]int {
|
||||||
|
var sets [][]int
|
||||||
|
|
||||||
|
var subsetsRec func(int)
|
||||||
|
|
||||||
|
var runningSet []int
|
||||||
|
subsetsRec = func(i int) {
|
||||||
|
if i == len(nums) {
|
||||||
|
toAppend := make([]int, len(runningSet))
|
||||||
|
copy(toAppend, runningSet)
|
||||||
|
|
||||||
|
sets = append(sets, toAppend)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Skip the current element
|
||||||
|
subsetsRec(i + 1)
|
||||||
|
|
||||||
|
// Include the current element
|
||||||
|
runningSet = append(runningSet, nums[i])
|
||||||
|
subsetsRec(i + 1)
|
||||||
|
runningSet = runningSet[:len(runningSet)-1]
|
||||||
|
}
|
||||||
|
|
||||||
|
subsetsRec(0)
|
||||||
|
return sets
|
||||||
|
}
|
Loading…
Reference in a new issue