Leetcode40 組合總和 II

2021-09-21 18:07:43 字數 1205 閱讀 5641

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

candidates中的每個數字在每個組合中只能使用一次。

說明:

示例 1:

輸入:candidates =[10,1,2,7,6,1,5], target =8,所求解集為:[

[1, 7],

[1, 2, 5],

[2, 6],

[1, 1, 6]

]

示例 2:

輸入:candidates = [2,5,2,1,2], target = 5,所求解集為:[

[1,2,2],

[5]]

public class combinationsum2 

private void backtrack(int candidates, int target, int start,

list> res, listtemp)

if (target == 0)

/** 最開始我是這樣去重的 但是發現會一起把重複元素組成的解去掉。比如:

* 輸入 : [10,1,2,7,6,1,5] 8

* 輸出 : [[1,2,5],[1,7],[2,6]]

* 缺少了 [1,1,6] 這個解

*//**

if (start != 0 && candidates[start] == candidates[start - 1])

**//*

* 從start到candidate末尾的元素來搜尋有沒有組成target的可能性

* target >= candidates[i]意思是,如果當前的元素已經大於target就不要往下搜尋了

*/for (int i = start; i < candidates.length && target >= candidates[i];i ++) else }}

}

leetcode 40 組合總和

給定乙個陣列candidates和乙個目標數target,找出candidates中所有可以使數字和為target的組合。candidates中的每個數字在每個組合中只能使用一次。說明 示例 1 輸入 candidates 10,1,2,7,6,1,5 target 8,所求解集為 1,7 1,2,...

leetcode40 組合總和 II

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

LeetCode 40 組合總和 II

給定乙個陣列candidates和乙個目標數target,找出candidates中所有可以使數字和為target的組合。candidates中的每個數字在每個組合中只能使用一次。說明 示例 1 輸入 candidates 10,1,2,7,6,1,5 target 8,所求解集為 1,7 1,2,...