求助,使用python解决一道回溯算法的题目时遇到的具体问题
代码如下,idx这个变量是怎么变的?求高人解答一下,感谢。
**点击查看代码**
class Solution(object):
def combinationSum(self, candidates, target):
"""
:type candidates: List[int]
:type target: int
:rtype: List[List[int]]
"""
result=[]
n=len(candidates)
self.backtracking(n,candidates,target,0,[],result)
return result
def backtracking(self,n,candidates,target,idx,path,result):
path_sum=sum(path)
# path_len=len(path)
# if path_len>=n and path_sum>target:
if path_sum>target:
return
if path_sum==target:
result.append(path[:])
return
for i in range(idx,n):
path.append(candidates[i])
self.backtracking(n,candidates,target,idx,path,result)
idx+=1
path.pop()

浙公网安备 33010602011771号