一、全排列
1.原题链接
链接: 46.全排列
给定一个不含重复数字的数组 nums ,返回其 所有可能的全排列 。你可以 按任意顺序 返回答案。
示例 1:
输入:nums = [1,2,3]
输出:[[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]]
示例 2:
输入:nums = [0,1]
输出:[[0,1],[1,0]]
示例 3:
输入:nums = [1]
输出:[[1]]
2.代码
class Solution {
List<Integer> nums;
List<List<Integer>> res;
void swap(int a, int b) {
int tmp = nums.get(a);
nums.set(a, nums.get(b));
nums.set(b, tmp);
}
void dfs(int x) {
if (x == nums.size() - 1) {
res.add(new ArrayList<>(nums));
return;
}
for (int i = x; i < nums.size(); i++) {
swap(i, x);
dfs(x + 1);
swap(i, x);
}
}
public List<List<Integer>> permute(int[] nums) {
this.res = new ArrayList<>();
this.nums = new ArrayList<>();
for (int num : nums) {
this.nums.add(num);
}
dfs(0);
return res;
}
}
二、组合总数
1.原题链接
链接: 39.组合总数
给你一个 无重复元素 的整数数组 candidates 和一个目标整数 target ,找出 candidates 中可以使数字和为目标数 target 的 所有 不同组合 ,并以列表形式返回。你可以按 任意顺序 返回这些组合。
candidates 中的 同一个 数字可以 无限制重复被选取 。如果至少一个数字的被选数量不同,则两种组合是不同的。
对于给定的输入,保证和为 target 的不同组合数少于 150 个。
示例 1:
输入:candidates = [2,3,6,7], target = 7
输出:[[2,2,3],[7]]
解释:
2 和 3 可以形成一组候选,2 + 2 + 3 = 7 。注意 2 可以使用多次。
7 也是一个候选, 7 = 7 。仅有这两种组合。
示例 2:
输入: candidates = [2,3,5], target = 8
输出: [[2,2,2,2],[2,3,3],[3,5]]
示例 3:
输入: candidates = [2], target = 1
输出: []
2.代码
class Solution {
void backtrack(List<Integer> state, int target, int[] candidates, int start, List<List<Integer>> res) {
if (target == 0) {//如果target为0说明找到和为target的组合state了,加入res
res.add(new ArrayList<>(state));
return;
}
for (int i = start; i < candidates.length; i++) {//i为start而不是0,防止出现重复的组合
if (target - candidates[i] < 0) {//说明此时组合总数已经大于target不满足条件了
break;
}
state.add(candidates[i]);
backtrack(state, target - candidates[i], candidates, i, res);
//撤销上一步的选择,即从 state中移除最后加入的元素,
//这样state就回到了上一步的状态,继续尝试下一个数字。
state.remove(state.size() - 1);
}
}
public List<List<Integer>> combinationSum(int[] candidates, int target) {
List<Integer> state = new ArrayList<>();//state就是一个组合,满足条件则插入res
Arrays.sort(candidates);
int start = 0;
List<List<Integer>> res = new ArrayList<>();
backtrack(state, target, candidates, start, res);
return res;
}
}
三、组合总数Ⅱ
1.原题链接
链接: 组合总和Ⅱ
2.代码
class Solution {
void backtrack(List<Integer> state, int target, int[] candidates, int start, List<List<Integer>> res) {
if (target == 0) {
res.add(new ArrayList<>(state));
return;
}
for (int i = start; i < candidates.length; i++) {
if (target - candidates[i] < 0) {
break;
}
//如果该元素与左边元素相等,说明该搜索分支重复,直接跳过
if (i > start && candidates[i] == candidates[i-1]) {
continue;
}
state.add(candidates[i]);
//这道题的backtrack()方法在进行下一轮选择时start位置为i+1而不是i,
//而39题组合总数的backtrack方法则是i。
//是因为这道题和上一道题的区别是不允许重复使用candidates中的数字,
//故我们在递归时只能使用下一个数字i+1。
backtrack(state, target - candidates[i], candidates, i + 1, res);
state.remove(state.size() - 1);
}
}
public List<List<Integer>> combinationSum2(int[] candidates, int target) {
List<Integer> state = new ArrayList<>();
Arrays.sort(candidates);
int start = 0;
List<List<Integer>> res = new ArrayList<>();
backtrack(state, target, candidates, start, res);
return res;
}
}
三、参考
k神:https://leetcode.cn/u/jyd/