組合總和II

2021-08-18 21:16:22 字數 721 閱讀 8750

給定乙個無重複元素的陣列 candidates 和乙個目標數 target ,找出 candidates 中所有可以使數字和為 target 的組合。

candidates 中的數字可以無限制重複被選取。

說明:所有數字(包括 target)都是正整數。

解集不能包含重複的組合。

示例 1:

輸入: candidates = [2,3,6,7], target = 7,

所求解集為:

[ [7],

[2,2,3]

] 示例 2:

輸入: candidates = [2,3,5], target = 8,

所求解集為:

[ [2,2,2,2],

[2,3,3],

[3,5]

]

思路描述:

1.其實採用的還是遞迴回溯的思想

2.對每個數都是使用或者不使用,計算和,滿足條件時新增到列表

class

solution

private void cal(list

> list, arraylisttemplist,

int candidates, int target,int cur,int begin) else

if(cur>target)else}}

}

組合總和 II

給定乙個陣列 candidates 和乙個目標數 target 找出 candidates 中所有可以使數字和為 target 的組合。candidates 中的每個數字在每個組合中只能使用一次。說明 所有數字 包括目標數 都是正整數。解集不能包含重複的組合。示例 1 輸入 candidates 1...

組合總和 II

問題描述 給定乙個陣列 candidates 和乙個目標數 target 找出 candidates 中所有可以使數字和為 target 的組合。candidates 中的每個數字在每個組合中只能使用一次。說明 所有數字 包括目標數 都是正整數。解集不能包含重複的組合。示例 1 輸入 candida...

組合總和II

組合總和ii 給定乙個陣列 candidates 和乙個目標數 target 找出 candidates 中所有可以使數字和為 target 的組合。candidates 中的每個數字在每個組合中只能使用一次。說明 1 所有數字 包括目標數 都是正整數。2 解集不能包含重複的組合。使用遞迴函式dfs...