I'm trying to learn Go but I can't figure it out why this code at the end of the recursion call stack returns an empty slice, any help? Also tmp
doesn't even seem to register in the debugger.
func main() {
input := [3]int{4, 6, 7}
// expected [[6,7],[4,6,7],[4,6],[4,7]]
fmt.Println(findSubsequences(input))
}
func findSubsequences(nums [3]int) [][]int {
res := [][]int{}
list := []int{}
findSubsequence(res, list, nums, 0)
return res
}
func findSubsequence(res [][]int, list []int, nums [3]int, id int) [][]int {
if len(list) > 1 {
tmp := make([]int, len(list))
copy(tmp, list)
res = append(res, tmp)
}
var unique []int
for i := id; i < len(nums); i++ {
if id > 0 && nums[i] < nums[id-1] {
continue // skip non-increase
}
if contains(unique, nums[i]) {
continue // skip duplicate
}
unique = append(unique, nums[i])
list = append(list, nums[i])
findSubsequence(res, list, nums, id+1)
list = list[:len(list)-1]
}
return res
}
func contains(s []int, e int) bool {
for _, a := range s {
if a == e {
return true
}
}
return false
}