感觉回溯的题一直挺难做的,记录一下。
文章目录
组合专题
从n个元素的集合,挑k个出来。
组合0-返回范围 [1, n] 中所有可能的 k 个数的组合
class Solution {
List<List<Integer>> ans = new ArrayList<>();
List<Integer> path = new ArrayList<>();
public List<List<Integer>> combine(int n, int k) {
if(n <= 0 || n < k) return ans;
backTrack(n,k,1);
return ans;
}
public void backTrack(int n, int k, int startIndex){
if(path.size() == k){
ans.add(new ArrayList<>(path));
return;
}
for(int i = startIndex; i <= n - (k-path.size())+1 ; i++){ //这里有一波剪枝操作
path.add(i);
backTrack(n, k, i+1);
path.remove(path.size()-1);
}
}
}
组合1-组合总和(可重复用)
给定一个无重复元素的正整数数组 candidates 和一个正整数 target ,找出 candidates 中所有可以使数字和为目标数 target 的组合。candidates 中的数字可以无限制重复被选取。
思路:回溯。注意因为可重复,递归的时候不是i+1,而是i。
class Solution {
List<List<Integer>> ans = new ArrayList<>();
List<Integer> path = new ArrayList<>();
public List<List<Integer>> combinationSum(int[] candidates, int target) {
backTrack(candidates, target, 0, 0);
return ans;
}
public void backTrack(int[] candidates, int target, int sum, int startIndex){
if(sum > target) return;
if(sum == target){
ans.add(new ArrayList(path));
return;
}
for(int i = startIndex; i < candidates.length; i++){
path.add(candidates[i]);
sum += candidates[i];
backTrack(candidates, target, sum, i);
path.remove(path.size()-1);
sum -= candidates[i];
}
}
}
组合2-组合总和(仅用一次)
40. 组合总和 II
给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合。candidates 中的每个数字在每个组合中只能使用一次。
思路:注意因为只能用一次,递归的时候是i+1;因为元素可能存在重复,为避免重复结果,先排序,回溯时如果遇到连续的值相等 的情况,只进行第一次,后面的continue。
class Solution {
List<List<Integer>> ans = new ArrayList<>();
List<Integer> path = new ArrayList<>();
public List<List<Integer>> combinationSum2(int[] candidates, int target) {
Arrays.sort(candidates);//先排序
backTrack(candidates, target, 0, 0);
return ans;
}
public void backTrack(int[] candidates, int target, int startIndex, int sum){
if(sum > target) return;
if(sum == target){
//if(!ans.contains(path))
ans.add(new ArrayList<>(path));
return;
}
for(int i = startIndex; i < candidates.length; i++){
if(i > startIndex && candidates[i] == candidates[i-1]) continue;//避免重复
path.add(candidates[i]);
sum += candidates[i];
backTrack(candidates, target, i+1, sum);
path.remove(path.size()-1);
sum -= candidates[i];
}
}
}
组合3-组合总和(限定元素个数)
216. 组合总和 III
找出所有相加之和为 n 的 k 个数的组合。组合中只允许含有 1 - 9 的正整数,并且每种组合中不存在重复的数字。
思路:回溯。元素不让重复选 -> i+1,元素本身不重复,不用排序啥的。限制一下结果不仅要满足和为n,还要满足长度为k。
class Solution {
List<List<Integer>> ans = new ArrayList<>();
List<Integer> path = new ArrayList<>();
public List<List<Integer>> combinationSum3(int k, int n) {
backTrack(k, n, 0, 1);
return ans;
}
void backTrack(int k, int n, int sum, int start_index){
if(sum > n || path.size() > k) return;
if(sum == n && path.size() == k){
ans.add(new ArrayList<>(path));
return;
}
for(int i = start_index; i <= 9; i++){
path.add(i);
sum += i;
backTrack(k, n, sum, i+1);
sum -= i;
path.remove(path.size()-1);
}
}
}
组合4-假排列问题
377. 组合总和 Ⅳ
大意了,用回溯直接超时。
给你一个由 不同 整数组成的数组 nums ,和一个目标整数 target 。请你从 nums 中找出并返回总和为 target 的元素组合的个数。
这种回溯的时候其实用不到start_index变量了,因为本质上他已不是组合问题,都允许换个顺序变成新结果了~
看到计算方案数了,要立马想到动态规划。
dp轻取。用dp[x]表示选取的元素之和等于 x 的方案数,目标是求 dp[target]。
动态规划的边界是dp[0]=1。只有当不选取任何元素时,元素之和才为 0,因此只有 1 种方案。
当1 ≤ i ≤ target 时,如果存在一种排列,其中的元素之和等于 i,则该排列的最后一个元素一定是数组 nums 中的一个元素。假设该排列的最后一个元素是 num,则一定有 num≤i,对于元素之和等于 i−num 的每一种排列,在最后添加 num 之后即可得到一个元素之和等于 i的排列,因此在计算dp[i] 时,应该计算所有的dp[i−num] 之和。
初始化dp[0]=1;
遍历 i 从 1 到target,对于每个 i,进行如下操作:
遍历数组 nums 中的每个元素 num,当num≤i 时,将dp[i−num] 的值加到 dp[i]。
最终得到dp[target] 的值即为答案。
class Solution {
public int combinationSum4(int[] nums, int target) {
int[] dp = new int[target+1];//用dp[x]表示选取的元素之和等于 x 的方案数
dp[0] = 1;
for(int i = 1; i <= target; i++){
for(int num : nums){
if(i >= num) dp[i] += dp[i-num];
}
}
return dp[target];
}
}
排列专题
不含重复元素的数组 全排列
class Solution {
List<Integer> path = new ArrayList<>();
List<List<Integer>> ans = new ArrayList<>();
public List<List<Integer>> permute(int[] nums) {
if(nums.length == 0) return ans;
boolean[] used = new boolean[nums.length];
backTrack(nums, used);
return ans;
}
public void backTrack(int[] nums, boolean[] used){
if(path.size() == nums.length){
ans.add(new ArrayList<>(path));
return;
}
for(int i = 0; i < nums.length; i++){
if(!used[i]){
path.add(nums[i]);
used[i] = true;
backTrack(nums, used);
path.remove(path.size()-1);
used[i] = false;
}
}
}
}