一、子集问题(没有重复元素)
给你一个整数数组 nums ,返回该数组所有可能的子集(幂集)。解集不能包含重复的子集。
示例 :
输入:nums = [1,2,3]
输出:[[],[1],[2],[1,2],[3],[1,3],[2,3],[1,2,3]]
public List<List<Integer>> subsets(int[] nums) {
List<List<Integer>> res = new ArrayList<>();
ArrayList<Integer> select = new ArrayList<Integer>();
backtrack(0, nums, res, select);
return res;
}
private void backtrack(int index, int[] nums, List<List<Integer>> res, ArrayList<Integer> select) {
res.add(new ArrayList<>(select));
for (int j = index; j < nums.length; j++) {
select.add(nums[j]);
backtrack(j + 1, nums, res, select);
select.remove(select.size() - 1);
}
}
二、子集问题(有重复元素去重需先排序再剪支)
给定一个可能包含重复元素的整数数组 nums,返回该数组所有可能的子集(幂集)。
说明:解集不能包含重复的子集。
示例:
输入: [1,2,2]
输出:
[
[2],
[1],
[1,2,2],
[2,2],
[1,2],
[]
]
public List<List<Integer>> subsetsWithDup(int[] nums) {
List<Integer> select = new ArrayList<Integer>();
List<List<Integer>> res = new ArrayList<List<Integer>>();
Arrays.sort(nums);
dfs(0, select, nums, res);
return res;
}
private void dfs(int index, List<Integer> select, int[] nums, List<List<Integer>> res) {
res.add(new ArrayList<>(select));
for (int i = index; i < nums.length; i++) {
//和上个数字相等就跳过
if (i > index && nums[i] == nums[i - 1]) {
continue;
}
select.add(nums[i]);
dfs2(i + 1, select, nums, res);
select.remove(select.size() - 1);
}
}
三、组合
给定两个整数 n 和 k,返回 1 … n 中所有可能的 k 个数的组合。
示例:
输入: n = 4, k = 2
输出:
[
[2,4],
[3,4],
[2,3],
[1,2],
[1,3],
[1,4],
]
public List<List<Integer>> combine(int n, int k) {
int[] nums = new int[n];
for (int i = 1; i <= n; i++) {
nums[i - 1] = i;
}
List<List<Integer>> res = new ArrayList<>();
List<Integer> select = new ArrayList<>();
combineDfs(0, k, nums, select, res);
return res;
}
private void combineDfs(int index, int k, int[] nums, List<Integer> select, List<List<Integer>> res) {
if (select.size() == k) {
res.add(new ArrayList<>(select));
return;
}
for (int i = index; i < nums.length; i++) {
select.add(nums[i]);
combineDfs(i + 1, k, nums, select, res);
select.remove(select.size() - 1);
}
}
四、组合2
给定一个无重复元素的数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合。
candidates 中的数字可以无限制重复被选取。
说明:
所有数字(包括 target)都是正整数。
解集不能包含重复的组合。
示例 1:
输入:candidates = [2,3,6,7], target = 7,
所求解集为:
[
[7],
[2,2,3]
]
public List<List<Integer>> combinationSum(int[] candidates, int target) {
List<List<Integer>> res = new ArrayList<>();
List<Integer> select = new ArrayList<>();
Arrays.sort(candidates);
Integer sum = 0;
combinationSumDfs(0, sum, candidates, target, select, res);
return res;
}
private void combinationSumDfs(int index, int sum, int[] candidates, int target, List<Integer> select, List<List<Integer>> res) {
if (sum == target) {
res.add(new ArrayList<>(select));
return;
} else if (sum > target) {
return;
}
for (int i = index; i < candidates.length; i++) {
select.add(candidates[i]);
sum = sum + candidates[i];
combinationSumDfs(i, sum, candidates, target, select, res);
Integer remove = select.remove(select.size() - 1);
sum = sum - remove;
}
}
五、组合3
给定一个数组 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]
]
public List<List<Integer>> combinationSum2(int[] candidates, int target) {
List<List<Integer>> res = new ArrayList<>();
List<Integer> select = new ArrayList<>();
Arrays.sort(candidates);
Integer sum = 0;
combinationSumDfs2(0, sum, candidates, target, select, res);
return res;
}
private void combinationSumDfs2(int index, int sum, int[] candidates, int target, List<Integer> select, List<List<Integer>> res) {
if (sum == target) {
res.add(new ArrayList<>(select));
return;
} else if (sum > target) {
return;
}
for (int i = index; i < candidates.length; i++) {
if(i > index && candidates[i] == candidates[i-1]){
continue;
}
select.add(candidates[i]);
sum = sum + candidates[i];
combinationSumDfs2(i+1, sum, candidates, target, select, res);
Integer remove = select.remove(select.size() - 1);
sum = sum - remove;
}
}
六、全排列
给定一个 没有重复 数字的序列,返回其所有可能的全排列。
示例:
输入: [1,2,3]
输出:
[
[1,2,3],
[1,3,2],
[2,1,3],
[2,3,1],
[3,1,2],
[3,2,1]
]
public List<List<Integer>> permute(int[] nums) {
List<List<Integer>> res = new ArrayList<>();
//决策树
LinkedList<Integer> track = new LinkedList<>();
//可用元素集合
boolean[] used = new boolean[nums.length];
backtrack(nums, used, track, res);
return res;
}
private void backtrack(int[] nums, boolean[] used, LinkedList<Integer> track, List<List<Integer>> res) {
if (track.size() == nums.length) {
res.addAll(Collections.singleton(new ArrayList<>(track)));
return;
}
for (int i = 0; i < nums.length; i++) {
// 排除不合法的选择
if (used[i]) {
continue;
}
// 做选择
track.addLast(nums[i]);
used[i] = true;
// 进入下一层决策树
backtrack(nums, used, track, res);
// 取消选择
track.removeLast();
used[i] = false;
}
}
七、全排列2(去重注意需先排序)
给定一个可包含重复数字的序列 nums ,按任意顺序 返回所有不重复的全排列。
示例 1:
输入:nums = [1,1,2]
输出:
[[1,1,2],
[1,2,1],
[2,1,1]]
public List<List<Integer>> permuteUnique(int[] nums) {
List<List<Integer>> res = new ArrayList<>();
//决策树
LinkedList<Integer> track = new LinkedList<>();
//可用元素集合
boolean[] used = new boolean[nums.length];
Arrays.sort(nums);
backtrack(nums, used, track, res);
return res;
}
private void backtrack(int[] nums, boolean[] used, LinkedList<Integer> track, List<List<Integer>> res) {
if (track.size() == nums.length) {
res.addAll(Collections.singleton(new ArrayList<>(track)));
return;
}
for (int i = 0; i < nums.length; i++) {
// 排除不合法的选择
if (used[i] || (i > 0 && nums[i] == nums[i - 1] && !used[i - 1])) {
continue;
}
// 做选择
track.addLast(nums[i]);
used[i] = true;
// 进入下一层决策树
backtrack(nums, used, track, res);
// 取消选择
track.removeLast();
used[i] = false;
}
}