本題和第77,39題一樣都可以用回溯算法進行解決泌辫。代碼的格式與前兩題很相似。
給定一個數(shù)組 candidates 和一個目標數(shù) target 择示,找出 candidates 中所有可以使數(shù)字和為 target 的組合。candidates 中的每個數(shù)字在每個組合中只能使用一次。
說明:
所有數(shù)字(包括目標數(shù))都是正整數(shù)榛丢。
解集不能包含重復的組合。
示例 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]
]
來源:力扣(LeetCode)
鏈接:https://leetcode-cn.com/problems/combination-sum-ii
List<List<Integer>> res = new ArrayList<>();
Deque<Integer> path = new ArrayDeque<>();
if(candidates.length < 0)
return res;
Arrays.sort(candidates); //剪枝挺庞,排序加強一些
backtrack(candidates,target,0,path,res);
return res;
}
public void backtrack(int[] candidates, int target, int begin, Deque<Integer> path, List<List<Integer>> res){
if(target < 0)
return;
if(target == 0 && !res.contains(t = new ArrayList<>(path))){
res.add(new ArrayList<>(path));
return;
}
for(int i = begin ; i < candidates.length ; i++){
//當target - candidates[i] < 0 就表示現(xiàn)在數(shù)之和已經(jīng)大于目標值晰赞,就不用再往下進行了
if (target - candidates[i] < 0){
break;
}
path.addLast(candidates[i]); //回溯開始之前將數(shù)字加進去
backtrack(candidates,target-candidates[i],i+1,path,res); //進入回溯注意還要從i開始。判斷是不是全是i還是有幾個i
path.removeLast(); //回溯之后要回到原狀態(tài)
}
}
還有一種可以在for循環(huán)中加入剪枝增加效率选侨,上面的代碼中的 if (target - candidates[i] < 0),就表示當前數(shù)字的值大于剩下的和(target)就不用再繼續(xù)進行掖鱼。還可以在加入小剪枝。liweiwei1419援制,如下所示:
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Deque;
import java.util.List;
public class Solution {
public List<List<Integer>> combinationSum2(int[] candidates, int target) {
int len = candidates.length;
List<List<Integer>> res = new ArrayList<>();
if (len == 0) {
return res;
}
// 關鍵步驟
Arrays.sort(candidates);
Deque<Integer> path = new ArrayDeque<>(len);
dfs(candidates, len, 0, target, path, res);
return res;
}
/**
* @param candidates 候選數(shù)組
* @param len 冗余變量
* @param begin 從候選數(shù)組的 begin 位置開始搜索
* @param target 表示剩余戏挡,這個值一開始等于 target,基于題目中說明的"所有數(shù)字(包括目標數(shù))都是正整數(shù)"這個條件
* @param path 從根結點到葉子結點的路徑
* @param res
*/
private void dfs(int[] candidates, int len, int begin, int target, Deque<Integer> path, List<List<Integer>> res) {
if (target == 0) {
res.add(new ArrayList<>(path));
return;
}
for (int i = begin; i < len; i++) {
// 大剪枝
if (target - candidates[i] < 0) {
break;
}
// 小剪枝
if (i > begin && candidates[i] == candidates[i - 1]) {
continue;
}
path.addLast(candidates[i]);
// 調(diào)試語句 ①
// System.out.println("遞歸之前 => " + path + "晨仑,剩余 = " + (target - candidates[i]));
// 因為元素不可以重復使用褐墅,這里遞歸傳遞下去的是 i + 1 而不是 i
dfs(candidates, len, i + 1, target - candidates[i], path, res);
path.removeLast();
// 調(diào)試語句 ②
// System.out.println("遞歸之后 => " + path + ",剩余 = " + (target - candidates[i]));
}
}
public static void main(String[] args) {
int[] candidates = new int[]{10, 1, 2, 7, 6, 1, 5};
int target = 8;
Solution solution = new Solution();
List<List<Integer>> res = solution.combinationSum2(candidates, target);
System.out.println("輸出 => " + res);
}
}
作者:liweiwei1419
鏈接:https://leetcode-cn.com/problems/combination-sum-ii/solution/hui-su-suan-fa-jian-zhi-python-dai-ma-java-dai-m-3/