记录一下自己学习回溯
List<Integer> list=new ArrayList<>();
List<List<Integer>>lists=new ArrayList<>();
public List<List<Integer>> combinationSum(int[] candidates, int target) {
dfs(0,candidates,target,0,list);
return lists;
}
void dfs(int cur,int []candidates,int target,int sum,List<Integer>list)
{
if(cur==candidates.length)return;
if(sum==target)
{
lists.add(new ArrayList<>(list));
return;
}
dfs(cur+1, candidates, target,sum,list);
if(sum+candidates[cur]<=target)
{
list.add(candidates[cur]);
dfs(cur, candidates, target,sum+candidates[cur],list);
list.remove(list.size()-1)
}
}
```