回溯之组合问题
关于回溯
回溯法也可以叫做回溯搜索法,是一种搜索的方式。回溯是递归的副产品,只要有递归就会有回溯。回溯算法是一种较为低效的算法,因为回溯的本质是穷举,穷举所有可能,然后选出想要的答案,如果想让回溯高效一些,可以加一些剪枝的操作,但也改不了回溯法就是穷举的本质。
回溯法解决的问题:
- 组合问题:N个数里面按一定规则找出k个数的集合
- 切割问题:一个字符串按一定规则有几种切割方式
- 子集问题:一个N个数的集合里有多少符合条件的子集
- 排列问题:N个数按一定规则全排列,有几种排列方式
- 棋盘问题:N皇后,解数独等等
注意:组合是不强调元素顺序的,排列是强调元素顺序。(组合无序,排列有序)
回溯法解决的问题都可以抽象为树形结构,回溯法解决的都是在集合中递归查找子集,集合的大小就构成了树的宽度,递归的深度,则构成树的深度。
回溯算法模板框架如下:
void backtrack(参数) { if (终止条件) { 存放结果; return; } for (选择:本层集合中元素(树中节点孩子的数量就是集合的大小)) { 处理节点; backtracking(路径,选择列表); // 递归 回溯,撤销处理结果 } }
77.组合
class Solution {
List<List<Integer>> result = new ArrayList();
List<Integer> path = new ArrayList();
public List<List<Integer>> combine(int n, int k) {
backtrack(n, k, 1);
return result;
}
public void backtrack(int n, int k, int startIndex) {
if (path.size() == k) {
result.add(new ArrayList(path));
return;
}
// 此处还可以进行一个剪枝操作
// 如果for循环选择的起始位置之后的元素个数 已经不足 需要的元素个数了,那么就没有必要搜索了。
// 以 n = 4, k = 4为例
// i <= n - (k - path.size()) + 1
for (int i = startIndex; i <= n - (k - path.size()) + 1; i++) {
path.add(i);
backtrack(n, k, i + 1);
path.remove(path.size() - 1);
}
}
}
216.组合总和
class Solution {
List<List<Integer>> result = new ArrayList();
List<Integer> path = new ArrayList();
public List<List<Integer>> combinationSum3(int k, int n) {
backTrack(k, n, 1, 0);
return result;
}
public void backTrack(int k, int n, int startIndex, int curSum) {
// 剪枝
if (curSum > n) return;
if (curSum == n && path.size() == k) {
result.add(new ArrayList(path));
return;
}
// 剪枝
for (int i = startIndex; i <= 9 - (k - path.size()) + 1; i++) {
path.add(i);
backTrack(k, n, i + 1, curSum + i);
path.remove(path.size() - 1);
}
}
}
17.电话号码的字母组合
class Solution {
List<String> result = new ArrayList();
StringBuffer sb = new StringBuffer();
public List<String> letterCombinations(String digits) {
if (digits == null || digits.length() == 0) return result;
Map<Character, String> map = new HashMap();
map.put('2', "abc");
map.put('3', "def");
map.put('4', "ghi");
map.put('5', "jkl");
map.put('6', "mno");
map.put('7', "pqrs");
map.put('8', "tuv");
map.put('9', "wxyz");
backTrack(digits, 0, map);
return result;
}
public void backTrack(String digits,int index, Map<Character, String> map) {
if (digits.length() == sb.length()) {
// result.add(new String(sb.toString()));
result.add(sb.toString());
return;
}
String cur = map.get(digits.charAt(index));
for (int i = 0; i < cur.length(); i++) {
sb.append(cur.charAt(i));
backTrack(digits, index + 1, map);
sb.deleteCharAt(sb.length() - 1);
}
}
}
39.组合总和
class Solution {
List<List<Integer>> result = new ArrayList();
List<Integer> path = new ArrayList();
public List<List<Integer>> combinationSum(int[] candidates, int target) {
Arrays.sort(candidates); // 对数组进行排序
backTrack(candidates, target, 0, 0);
return result;
}
public void backTrack(int[] candidates, int target, int startIndex, int sum) {
if (target == sum) {
result.add(new ArrayList(path));
return;
}
for (int i = startIndex; i < candidates.length; i++) {
int temp = candidates[i];
if (sum + temp > target) break; // 和大于target就没有必要进行后续的遍历了。必须要维持数组有序,否则会少情况
path.add(temp);
backTrack(candidates, target, i, sum + temp);
path.remove(path.size() - 1);
}
}
}
40.组合去重Ⅱ
// 我的错误做法
class Solution {
// 把所有组合求出来,再用set或者map去重,这么做很容易超时!
Set<List<Integer>> result = new HashSet();
List<Integer> path = new ArrayList();
public List<List<Integer>> combinationSum2(int[] candidates, int target) {
Arrays.sort(candidates);
backTrack(candidates, target, 0, 0);
List<List<Integer>> list = new ArrayList();
for (List<Integer> temp : result) {
list.add(temp);
}
return list;
}
public void backTrack(int[] candidates, int target, int startIndex, int sum) {
if (sum == target) {
result.add(new ArrayList(path));
return;
}
for (int i = startIndex; i < candidates.length; i++) {
int temp = candidates[i];
if (temp + sum > target) break;
path.add(temp);
backTrack(candidates, target, i + 1, temp + sum);
path.remove(path.size() - 1);
}
}
}
class Solution {
List<List<Integer>> result = new ArrayList();
List<Integer> path = new ArrayList();
public List<List<Integer>> combinationSum2(int[] candidates, int target) {
Arrays.sort(candidates);
Boolean[] used = new Boolean[candidates.length];
for (int i = 0; i < used.length; i++) used[i] = false;
backTrack(candidates, target, 0, 0, used);
return result;
}
public void backTrack(int[] candidates, int target, int startIndex, int sum, Boolean[] used) {
if (sum == target) {
result.add(new ArrayList(path));
return;
}
for (int i = startIndex; i < candidates.length; i++) {
int temp = candidates[i];
if (temp + sum > target) break;
// used[i - 1] == true,说明同一树枝candidates[i - 1]使用过
// used[i - 1] == false,说明同一树层candidates[i - 1]使用过
// 要避免的是同一树层中的相同元素被重复使用
if (i > 0 && candidates[i] == candidates[i-1] && !used[i-1]) {
continue;
}
path.add(temp);
used[i] = true;
backTrack(candidates, target, i + 1, temp + sum, used);
path.remove(path.size() - 1);
used[i] = false;
}
}
}
131.分割回文串
class Solution {
List<List<String>> result = new ArrayList();
List<String> path = new ArrayList();
public List<List<String>> partition(String s) {
backTrack(s, 0);
return result;
}
public void backTrack(String s, int startIndex) {
if (startIndex >= s.length()) {
result.add(new ArrayList(path));
return;
}
for (int i = startIndex; i < s.length(); i++) {
if (isPalindrome(s, startIndex, i)) {
String temp = s.substring(startIndex, i + 1);
path.add(temp);
} else {
continue;
}
backTrack(s, i + 1);
path.remove(path.size() - 1);
}
}
// 判断是否是回文串
public boolean isPalindrome(String s, int left, int right) {
while (left < right) {
if (s.charAt(left) != s.charAt(right)) return false;
left++;
right--;
}
return true;
}
}
93.复原 IP 地址(有点复杂)
class Solution {
List<String> result = new ArrayList();
public List<String> restoreIpAddresses(String s) {
backTrack(s, 0, 0);
return result;
}
public void backTrack(String s, int startIndex, int pointNum) {
if (pointNum == 3) {
if (isValid(s, startIndex, s.length())) result.add(s);
return;
}
for (int i = startIndex; i < s.length(); i++) {
if (isValid(s, startIndex, i + 1)) {
s = s.substring(0, i + 1) + "." + s.substring(i + 1);
pointNum++;
backTrack(s, i + 2, pointNum);
pointNum--;
s = s.substring(0, i + 1) + s.substring(i + 2);
} else {
break;
}
}
}
// 段位以0为开头的数字不合法
// 段位里有非正整数字符不合法
// 段位如果大于255了不合法
public boolean isValid(String s, int start, int end) {
if (start >= end) {
return false;
}
if (s.charAt(start) == '0' && (end - start > 1)) return false;
int num = 0;
for (int i = start; i < end; i++) {
if (s.charAt(i) < '0' || s.charAt(i) > '9') return false;
num = num * 10 + (s.charAt(i) - '0');
if (num > 255) return false;
}
return true;
}
}
如果把子集问题、组合问题、分割问题都抽象为一棵树的话,那么组合问题和分割问题都是收集树的叶子节点,而子集问题是找树的所有节点!
78. 子集
class Solution {
List<List<Integer>> result = new ArrayList();
List<Integer> path = new ArrayList();
public List<List<Integer>> subsets(int[] nums) {
backTrack(nums, 0);
return result;
}
public void backTrack(int[] nums, int startIndex) {
result.add(new ArrayList(path));
for (int i = startIndex; i < nums.length; i++) {
path.add(nums[i]);
backTrack(nums, i + 1);
path.remove(path.size() - 1);
}
}
}
90.子集Ⅱ
class Solution {
List<List<Integer>> result = new ArrayList();
List<Integer> paths = new ArrayList();
public List<List<Integer>> subsetsWithDup(int[] nums) {
Arrays.sort(nums);
Boolean[] used = new Boolean[nums.length];
for (int i = 0; i < used.length; i++) used[i] = false;
backTrack(nums, used, 0);
return result;
}
public void backTrack(int[] nums, Boolean[] used, int startIndex) {
result.add(new ArrayList(paths));
for (int i = startIndex; i < nums.length; i++) {
if (i > 0 && nums[i] == nums[i-1] && used[i-1] == false) continue;
paths.add(nums[i]);
used[i] = true;
backTrack(nums, used, i + 1);
paths.remove(paths.size() - 1);
used[i] = false;
}
}
}
491.递增子序列
class Solution {
List<List<Integer>> result = new ArrayList();
List<Integer> paths = new ArrayList();
public List<List<Integer>> findSubsequences(int[] nums) {
backTrack(nums, 0);
return result;
}
public void backTrack(int[] nums, int startIndex) {
if (paths.size() >= 2) {
result.add(new ArrayList(paths));
}
int[] used = new int[201];
for (int i = startIndex; i < nums.length; i++) {
if ((paths.isEmpty() || nums[i] >= paths.get(paths.size() - 1)) && used[nums[i] + 100] == 0) {
used[nums[i] + 100] = 1;
paths.add(nums[i]);
backTrack(nums, i + 1);
paths.remove(paths.size() - 1);
} else {
continue;
}
}
}
}
46.全排列
class Solution {
List<List<Integer>> result = new ArrayList();
List<Integer> paths = new ArrayList();
public List<List<Integer>> permute(int[] nums) {
Boolean[] used = new Boolean[nums.length];
for (int i = 0; i < used.length; i++) used[i] = false;
backTrack(nums, used);
return result;
}
public void backTrack(int[] nums, Boolean[] used) {
if (paths.size() == nums.length) {
result.add(new ArrayList(paths));
return;
}
for (int i = 0; i < nums.length; i++) {
// used数组,记录此时path里都有哪些元素使用了,一个排列里一个元素只能使用一次
if (!used[i]) {
paths.add(nums[i]);
used[i] = true;
backTrack(nums, used);
paths.remove(paths.size() - 1);
used[i] = false;
} else {
continue;
}
}
}
}
47.全排列Ⅱ
// 双重去重
class Solution {
List<List<Integer>> result = new ArrayList();
List<Integer> paths = new ArrayList();
public List<List<Integer>> permuteUnique(int[] nums) {
Arrays.sort(nums);
Boolean[] used = new Boolean[nums.length];
for (int i = 0; i < used.length; i++) used[i] = false;
backTrack(nums, used);
return result;
}
public void backTrack(int[] nums, Boolean[] used) {
if (paths.size() == nums.length) {
result.add(new ArrayList(paths));
return;
}
for (int i = 0; i < nums.length; i++) {
// 如果要对树层中前一位去重,就用used[i - 1] == false -> 效率更高
// 如果要对树枝前一位去重,用used[i - 1] == true
if (i > 0 && nums[i] == nums[i - 1] && !used[i - 1]) continue;
if (used[i] == false) {
used[i] = true;
paths.add(nums[i]);
backTrack(nums, used);
used[i] = false;
paths.remove(paths.size() - 1);
}
}
}
}