目录
回溯专题
77. 组合
一、题目描述
二、解题思路
回溯法
三、AC代码
class Solution {
List<List<Integer>> res = new ArrayList<>();
LinkedList<Integer> path = new LinkedList<>();
public List<List<Integer>> combine(int n, int k) {
backTrack(n, k, 1);
return res;
}
private void backTrack(int n, int k, int startIndex) {
if (path.size() == k) {
res.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.removeLast();
}
}
}
39.组合总和
一、题目描述
二、解题思路
回溯法
三、AC代码
class Solution {
List<List<Integer>> res = new ArrayList<>();
LinkedList<Integer> path = new LinkedList<>();
public List<List<Integer>> combinationSum(int[] candidates, int target) {
Arrays.sort(candidates);
backTrack(candidates, 0, target, 0);
return res;
}
private void backTrack(int[] candidates, int sum, int target, int idx) {
if (sum == target) {
res.add(new ArrayList<>(path));
return;
}
for (int i = idx; i < candidates.length; i++) {
if (sum + candidates[i] > target) {
break;
}
path.add(candidates[i]);
backTrack(candidates, sum+candidates[i], target, i);
path.removeLast();
}
}
}
123.组合总和II
一、题目描述
二、解题思路
回溯法
三、AC代码
class Solution {
List<List<Integer>> res = new ArrayList<>();
LinkedList<Integer> path = new LinkedList<>();
boolean[] used;
public List<List<Integer>> combinationSum2(int[] candidates, int target) {
Arrays.sort(candidates);
used = new boolean[candidates.length];
backTrack(candidates, 0, target, 0);
return res;
}
private void backTrack(int[] candidates, int sum, int target, int idx) {
if (sum == target) {
res.add(new ArrayList(path));
return;
}
for (int i = idx; i < candidates.length; i++) {
if (sum + candidates[i] > target) {
break;
}
if (i > 0 && candidates[i] == candidates[i-1] && !used[i-1]) {
continue;
}
used[i] = true;
path.add(candidates[i]);
backTrack(candidates, sum+candidates[i], target, i+1);
path.removeLast();
used[i] = false;
}
}
}
216.组合总和 III
一、题目描述
二、解题思路
回溯法
三、AC代码
class Solution {
List<List<Integer>> res = new ArrayList<>();
LinkedList<Integer> path = new LinkedList<>();
public List<List<Integer>> combinationSum3(int k, int n) {
backTrack(n, k, 1, 0);
return res;
}
private void backTrack(int targetSum, int k, int startIndex, int sum) {
if (path.size() == k) {
if (sum == targetSum) {
res.add(new ArrayList<>(path));
}
return;
}
for (int i = startIndex; i <= 9 - (k - path.size()) + 1; i++) {
if (sum + i > targetSum) {
break;
}
path.add(i);
backTrack(targetSum, k, i + 1, sum+i);
path.removeLast();
}
}
}
46. 全排列
一、题目描述
二、解题思路
回溯法
三、AC代码
class Solution {
List<List<Integer>> res = new ArrayList<>();
LinkedList<Integer> path = new LinkedList<>();
boolean[] used;
public List<List<Integer>> permute(int[] nums) {
used = new boolean[nums.length];
backTrack(nums);
return res;
}
private void backTrack(int[] nums){
if (path.size() == nums.length){
res.add(new ArrayList<>(path));
return;
}
for (int i = 0; i < nums.length; i++){
if (used[i]){
continue;
}
used[i] = true;
path.add(nums[i]);
backTrack(nums);
path.removeLast();
used[i] = false;
}
}
}
控制台
47.全排列 II
一、题目描述
二、解题思路
回溯法
三、AC代码
class Solution {
List<List<Integer>> res = new ArrayList<>();
LinkedList<Integer> path = new LinkedList<>();
boolean[] used;
public List<List<Integer>> permuteUnique(int[] nums) {
used = new boolean[nums.length];
Arrays.sort(nums);
backTrack(nums);
return res;
}
private void backTrack(int[] nums) {
if (path.size() == nums.length) {
res.add(new ArrayList<>(path));
return;
}
for (int i = 0; i < nums.length; i++) {
if (i > 0 && nums[i] == nums[i - 1] && !used[i - 1]) {
continue;
}
if (used[i]) {
continue;
}
used[i] = true;
path.add(nums[i]);
backTrack(nums);
path.removeLast();
used[i] = false;
}
}
}
78.子集
一、题目描述
二、解题思路
回溯法
三、AC代码
class Solution {
List<List<Integer>> res = new ArrayList<>();
LinkedList<Integer> path = new LinkedList<>();
public List<List<Integer>> subsets(int[] nums) {
backtrack(nums, 0);
return res;
}
private void backtrack(int[] nums, int startIndex){
res.add(new ArrayList<>(path));
if (startIndex >= nums.length){
return;
}
for (int i = startIndex; i < nums.length; i++){
path.add(nums[i]);
backtrack(nums, i + 1);
path.removeLast();
}
}
}
90.子集II
一、题目描述
二、解题思路
回溯法
三、AC代码
class Solution {
List<List<Integer>> res = new ArrayList<>();
LinkedList<Integer> path = new LinkedList<>();
boolean[] used;
public List<List<Integer>> subsetsWithDup(int[] nums) {
Arrays.sort(nums);
used = new boolean[nums.length];
backTrack(nums, 0);
return res;
}
private void backTrack(int[] nums, int startIndex){
res.add(new ArrayList<>(path));
if (startIndex >= nums.length){
return;
}
for (int i = startIndex; i < nums.length; i++){
if (i > 0 && nums[i] == nums[i - 1] && !used[i - 1]){
continue;
}
path.add(nums[i]);
used[i] = true;
backTrack(nums, i + 1);
path.removeLast();
used[i] = false;
}
}
}
17.电话号码的字母组合
一、题目描述
二、解题思路
回溯法
三、AC代码
class Solution {
List<String> res = new ArrayList<>();
StringBuilder tmp = new StringBuilder();
public List<String> letterCombinations(String digits) {
String[] numString = {"", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
backTrack(digits, numString, 0);
return res;
}
public void backTrack(String digits, String[] numString, int num) {
if (num == digits.length()) {
if (digits.length() != 0) {
res.add(tmp.toString());
}
return;
}
String str = numString[digits.charAt(num) - '0'];
for (int i = 0; i < str.length(); i++) {
tmp.append(str.charAt(i));
backTrack(digits, numString, num+1);
tmp.deleteCharAt(tmp.length()-1);
}
}
}
131.分割回文串
一、题目描述
二、解题思路
回溯法
三、AC代码
参考代码随想录:https://programmercarl.com/