为什么我们在每次回溯迭代结束时从列表中弹出?

问题描述

我有以下问题的解决方案:https://leetcode.com/problems/combinations/

List[List[int]]:
        def backtrack(first = 1,curr = []):
            # if the combination is done
            if len(curr) == k:  
                output.append(curr[:])
            for i in range(first,n + 1):
                # add i into the current combination
                curr.append(i)
                # use next integers to complete the combination
                backtrack(i + 1,curr)
                # backtrack
                curr.pop()
        
        output = []
        backtrack()
        return output

我的问题是关于 curr.pop() 为什么我们每次迭代都弹出 curr 组合?不应该有一些条件,比如 curr 已经在输出中吗?

另一个问题是关于递归调用 backtrack(i+1,curr) - 当调用它时,我说'i+1' 代替了主函数中的'first' 是否正确?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)