尝试将切片(阵列)存储在递归中的2D切片(阵列)中

发布于 2025-01-23 04:00:38 字数 1485 浏览 0 评论 0原文

试图通过递归并找到子序列并将子序列切片(数组)在递归中的2D切片(数组)中进行,但在执行后获得NULL 2D切片后,将组合总和问题存储。对于组合总和问题( https://leetcode.com/problems/problems/combination-combination-sum/

通过地图,这是可能的

    package main
    
    import "fmt"
    
    func main() {
        sourceArr := []int{4, 8, 10, 17, 1, 2, 7}
        subSeq := make([]int, 0, len(sourceArr))
        arrayOfArr := make([][]int, 0)
        // i'm calling fuction passing the arguments
        FindCombinationV2(sourceArr, subSeq, 10, 0, arrayOfArr)
        //after execution i'm getting null array's in 2D array
        fmt.Printf("arrayOfArr: %v\n", arrayOfArr)
    }
    func FindCombinationV2(arr, subSequenceArr []int, target, index int, ans [][]int) {
        if index == len(arr) {
            if target == 0 {
                // i'm getting sub sequences of actual target sum
                // here i'm appending subSequence slice in ans 2D slice
                ans = append(ans, [][]int{subSequenceArr}...)
            }
            return
        }
        if target >= arr[index] {
            subSequenceArr = append(subSequenceArr, arr[index])
            FindCombinationV2(arr, subSequenceArr, target-arr[index], index, ans)
            subSequenceArr = subSequenceArr[:len(subSequenceArr)-1]
        }
    
        FindCombinationV2(arr, subSequenceArr, target, index+1, ans)
    
    }

Trying to perform combination sum problem through recursion and finding subsequences and storing subsequences slice (array) in 2D slice (array) in recursion but after execution getting null 2D slice. For combination sum problem (https://leetcode.com/problems/combination-sum/)

Through the map it's possible but I wanted to know how we can do same with the help of 2D Slice (array) [][]int

    package main
    
    import "fmt"
    
    func main() {
        sourceArr := []int{4, 8, 10, 17, 1, 2, 7}
        subSeq := make([]int, 0, len(sourceArr))
        arrayOfArr := make([][]int, 0)
        // i'm calling fuction passing the arguments
        FindCombinationV2(sourceArr, subSeq, 10, 0, arrayOfArr)
        //after execution i'm getting null array's in 2D array
        fmt.Printf("arrayOfArr: %v\n", arrayOfArr)
    }
    func FindCombinationV2(arr, subSequenceArr []int, target, index int, ans [][]int) {
        if index == len(arr) {
            if target == 0 {
                // i'm getting sub sequences of actual target sum
                // here i'm appending subSequence slice in ans 2D slice
                ans = append(ans, [][]int{subSequenceArr}...)
            }
            return
        }
        if target >= arr[index] {
            subSequenceArr = append(subSequenceArr, arr[index])
            FindCombinationV2(arr, subSequenceArr, target-arr[index], index, ans)
            subSequenceArr = subSequenceArr[:len(subSequenceArr)-1]
        }
    
        FindCombinationV2(arr, subSequenceArr, target, index+1, ans)
    
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文