原则:
- 回溯函数需要path、ans参数
- 允许包含重复数字,则处理前一定要先排序
- 只有全排列需要记录
used[i],因为需要区分重复的元素
数字 n 代表生成括号的对数,请你设计一个函数,用于能够生成所有可能的并且 有效的 括号组合。
示例 1:
输入:n = 3
输出:[“((()))”,”(()())”,”(())()”,”()(())”,”()()()”]
示例 2:
输入:n = 1
输出:[“()”]
提示:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| class Solution { public List<String> generateParenthesis(int n) { List<String> ans = new ArrayList<>(); backtrack(n, 0, 0, new StringBuilder(), ans); return ans; }
public void backtrack(int n, int left, int right, StringBuilder path, List<String> ans) { if (left == right && left == n) { ans.add(path.toString()); } if (left < n) { path.append('('); backtrack(n, left + 1, right, path, ans); path.deleteCharAt(path.length() - 1); } if (left > right) { path.append(')'); backtrack(n, left, right + 1, path, ans); path.deleteCharAt(path.length() - 1); } } }
|
给你一个整数数组 nums ,数组中的元素 互不相同 。返回该数组所有可能的子集(幂集)。
解集 不能 包含重复的子集。你可以按 任意顺序 返回解集。
示例 1:
输入:nums = [1,2,3]
输出:[[],[1],[2],[1,2],[3],[1,3],[2,3],[1,2,3]]
示例 2:
输入:nums = [0]
输出:[[],[0]]
提示:
- 1 <= nums.length <= 10
- -10 <= nums[i] <= 10
nums 中的所有元素 互不相同
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| class Solution { public List<List<Integer>> subsets(int[] nums) { List<List<Integer>> ans = new ArrayList<>(); backtrack(nums, 0, new ArrayList<>(), ans); return ans; }
private void backtrack(int[] nums, int start, List<Integer> path, List<List<Integer>> ans) { ans.add(new ArrayList<>(path)); for (int i = start; i < nums.length; i++) { path.add(nums[i]); backtrack(nums, i + 1, path, ans); path.remove(path.size() - 1); } } }
|
给你一个整数数组 nums ,其中可能包含重复元素,请你返回该数组所有可能的子集(幂集)。
解集 不能 包含重复的子集。返回的解集中,子集可以按 任意顺序 排列。
示例 1:
输入:nums = [1,2,2]
输出:[[],[1],[1,2],[1,2,2],[2],[2,2]]
示例 2:
输入:nums = [0]
输出:[[],[0]]
提示:
- 1 <= nums.length <= 10
- -10 <= nums[i] <= 10
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| class Solution { public List<List<Integer>> subsetsWithDup(int[] nums) { List<List<Integer>> ans = new ArrayList<>(); Arrays.sort(nums); backtrack(nums, 0, new ArrayList<>(), ans); return ans; }
private void backtrack(int[] nums, int start, List<Integer> path, List<List<Integer>> ans) { ans.add(new ArrayList<>(path)); for (int i = start; i < nums.length; i++) { if (i > start && nums[i] == nums[i - 1]) { continue; }
path.add(nums[i]); backtrack(nums, i + 1, path, ans); path.remove(path.size() - 1); } } }
|
给定一个不含重复数字的数组 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]]
提示:
- 1 <= nums.length <= 6
- -10 <= nums[i] <= 10
nums 中的所有整数互不相同
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| class Solution { public List<List<Integer>> permute(int[] nums) { List<List<Integer>> ans = new ArrayList<>(); backtrack(nums, ans, new ArrayList<>()); return ans; }
private void backtrack(int[] nums, List<List<Integer>> ans, List<Integer> path) { if (path.size() == nums.length) { ans.add(new ArrayList<>(path)); return; } for (int i = 0; i < nums.length; i++) { if (path.contains(nums[i])) { continue; } path.add(nums[i]); backtrack(nums, ans, path); path.remove(path.size() - 1); } } }
|
给定一个可包含重复数字的序列 nums , 按任意顺序 返回所有不重复的全排列。
示例 1:
输入:nums = [1,1,2]
输出:[[1,1,2],[1,2,1],[2,1,1]]
示例 2:
输入:nums = [1,2,3]
输出:[[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]]
提示:
- 1 <= nums.length <= 8
- -10 <= nums[i] <= 10
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
| class Solution { public List<List<Integer>> permuteUnique(int[] nums) { List<List<Integer>> ans = new ArrayList<>(); Arrays.sort(nums); backtrack(nums, ans, new ArrayList<>(), new boolean[nums.length]); return ans; }
private void backtrack(int[] nums, List<List<Integer>> ans, List<Integer> path, boolean[] used) { if (nums.length == path.size()) { ans.add(new ArrayList<>(path)); } for (int i = 0; i < nums.length; i++) { if (used[i] || (i > 0 && used[i - 1] == false && nums[i] == nums[i - 1])) { continue; } used[i] = true; path.add(nums[i]); backtrack(nums, ans, path, used); used[i] = false; path.remove(path.size() - 1); } } }
|
给你一个 无重复元素 的整数数组 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
输出: []
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| class Solution { public List<List<Integer>> combinationSum(int[] candidates, int target) { List<List<Integer>> ans = new ArrayList<>(); backtrack(candidates, target, 0, new ArrayList<Integer>(), ans); return ans; }
public void backtrack(int[] candidates, int remain, int index, List<Integer> path, List<List<Integer>> ans) { if(remain < 0) return; if (remain == 0) { ans.add(new ArrayList(path)); } for (int i = index; i < candidates.length; i++) { path.add(candidates[i]); backtrack(candidates, remain - candidates[i], i, path, ans); path.remove(path.size() - 1); } } }
|
给定一个候选人编号的集合 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合。
candidates 中的每个数字在每个组合中只能使用 **一次 **。
注意:解集不能包含重复的组合。
示例 1:
输入: candidates = [10,1,2,7,6,1,5], target = 8,
输出:
[
[1,1,6],
[1,2,5],
[1,7],
[2,6]
]
示例 2:
输入: candidates = [2,5,2,1,2], target = 5,
输出:
[
[1,2,2],
[5]
]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
| class Solution { public List<List<Integer>> combinationSum2(int[] candidates, int target) { List<List<Integer>> ans = new ArrayList<>(); Arrays.sort(candidates); backtrack(candidates, target, 0, new ArrayList<>(), ans); return ans; }
public void backtrack(int[] nums, int remain, int index, List<Integer> path, List<List<Integer>> ans) { if(remain < 0){ return; } if (remain == 0) { ans.add(new ArrayList<>(path)); } for (int i = index; i < nums.length; i++) { if (i > index && nums[i] == nums[i - 1]) { continue; } if (nums[i] > remain) { break; } path.add(nums[i]); backtrack(nums, remain - nums[i], i + 1, path, ans); path.remove(path.size() - 1); } } }
|