7.7 二刷 回溯
This commit is contained in:
parent
b8f430ad10
commit
ce64f93c76
@ -23,7 +23,30 @@ import java.util.List;
|
||||
|
||||
* 链接:https://leetcode.cn/problems/combination-sum/
|
||||
*/
|
||||
//二刷会做
|
||||
public class CombinationSum {
|
||||
void dfs(List<List<Integer>> res,List<Integer> path,int[] candidates,int begin,int target,int cur){
|
||||
if(cur>target)return;
|
||||
else if (cur==target) {
|
||||
res.add(new ArrayList<>(path));
|
||||
return;
|
||||
}else {
|
||||
for (int i = begin; i < candidates.length; i++) {
|
||||
path.add(candidates[i]);
|
||||
cur+=candidates[i];
|
||||
dfs(res,path,candidates,i,target,cur);
|
||||
cur-=candidates[i];
|
||||
path.remove(path.size()-1);
|
||||
}
|
||||
}
|
||||
}
|
||||
public List<List<Integer>> combinationSum1(int[] candidates, int target) {
|
||||
List<List<Integer>> res=new ArrayList<>();
|
||||
List<Integer> path=new ArrayList<>();
|
||||
dfs(res,path,candidates,0,target,0);
|
||||
return res;
|
||||
}
|
||||
|
||||
void dfs(int[] candidates,List<Integer>path,List<List<Integer>>res,int start,int cur,int target){
|
||||
if(cur==target) {
|
||||
res.add(new ArrayList<>(path));
|
||||
|
@ -26,7 +26,34 @@ import java.util.List;
|
||||
|
||||
* 链接:https://leetcode.cn/problems/combination-sum-ii/
|
||||
*/
|
||||
//二刷会做
|
||||
public class CombinationSum2 {
|
||||
void dfs(List<List<Integer>> res,List<Integer> path,int[] candidates,int begin,int target,int cur){
|
||||
if(cur>target)return;
|
||||
else if (cur==target) {
|
||||
res.add(new ArrayList<>(path));
|
||||
return;
|
||||
}else {
|
||||
for (int i = begin; i < candidates.length; i++) {
|
||||
if(i>begin&&candidates[i]==candidates[i-1])continue;
|
||||
path.add(candidates[i]);
|
||||
cur+=candidates[i];
|
||||
dfs(res,path,candidates,i+1,target,cur);
|
||||
cur-=candidates[i];
|
||||
path.remove(path.size()-1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public List<List<Integer>> combinationSum22(int[] candidates, int target) {
|
||||
Arrays.sort(candidates);
|
||||
List<List<Integer>> res=new ArrayList<>();
|
||||
List<Integer> path=new ArrayList<>();
|
||||
dfs(res,path,candidates,0,target,0);
|
||||
return res;
|
||||
}
|
||||
|
||||
|
||||
void backtrack(List<List<Integer>>res,List<Integer>path,int[] candidates,int[]visited,int start,int cur,int target){
|
||||
if(cur==target) {
|
||||
res.add(new ArrayList<>(path));
|
||||
|
@ -19,7 +19,31 @@ import java.util.List;
|
||||
|
||||
* 链接:https://leetcode.cn/problems/combination-sum-iii/
|
||||
*/
|
||||
//二刷会做
|
||||
public class CombinationSum3 {
|
||||
void dfs(List<List<Integer>> res,List<Integer> path,int k,int n,int cnt,int sum,int begin){
|
||||
if(cnt==k){
|
||||
if(n==sum)res.add(new ArrayList<>(path));
|
||||
}else {
|
||||
for (int i = begin; i <= 9; i++) {
|
||||
path.add(i);
|
||||
cnt++;
|
||||
sum+=i;
|
||||
dfs(res,path,k,n,cnt,sum,i+1);
|
||||
sum-=i;
|
||||
cnt--;
|
||||
path.remove(path.size()-1);
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
public List<List<Integer>> combinationSum33(int k, int n) {
|
||||
List<List<Integer>> res=new ArrayList<>();
|
||||
List<Integer> path=new ArrayList<>();
|
||||
dfs(res,path,k,n,0,0,1);
|
||||
return res;
|
||||
}
|
||||
|
||||
void dfs(int n,int k,int used,List<List<Integer>>res,List<Integer>path,int cursum,int begin){
|
||||
if(used==k){
|
||||
if(n==cursum)
|
||||
|
@ -15,7 +15,9 @@ package backtrack;
|
||||
|
||||
* 链接:https://leetcode.cn/problems/word-search/
|
||||
*/
|
||||
//二刷会做
|
||||
public class Exist {
|
||||
|
||||
boolean flag = false;
|
||||
|
||||
void dfs(char[][] board, String word, int i, int j, int index) {
|
||||
|
@ -18,7 +18,39 @@ import java.util.List;
|
||||
* 链接:https://leetcode.cn/problems/generate-parentheses/
|
||||
*/
|
||||
//做出来了,但我的写法很丑,改为标准答案
|
||||
//二刷不会
|
||||
public class GenerateParenthesis {
|
||||
|
||||
//自己写的
|
||||
class Solution {
|
||||
public List<String> generateParenthesis(int n) {
|
||||
List<String> res = new ArrayList<>();
|
||||
backtrack(res, new StringBuilder(), 0, 0, 2 * n);
|
||||
return res;
|
||||
}
|
||||
|
||||
private void backtrack(List<String> res, StringBuilder path,
|
||||
int idx, int bias, int len) {
|
||||
|
||||
if (bias < 0 || bias > len - idx) return; // 提前剪枝 当前右括号数量多或者剩余右括号不足以抵消左括号时..
|
||||
if (idx == len) { // 成功
|
||||
res.add(path.toString());
|
||||
return;
|
||||
}
|
||||
|
||||
int oldLen = path.length();
|
||||
|
||||
path.append('('); // 放左括号 bias+1
|
||||
backtrack(res, path, idx + 1, bias + 1, len);
|
||||
path.setLength(oldLen);
|
||||
|
||||
path.append(')'); // 放右括号 bias-1
|
||||
backtrack(res, path, idx + 1, bias - 1, len);
|
||||
path.setLength(oldLen);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// 回溯函数
|
||||
void dfs(List<String> res, StringBuilder path, int left, int right, int n) {
|
||||
// 如果左右括号都用完了,加入结果
|
||||
|
@ -2,6 +2,7 @@ package backtrack;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
* 题目: 17. 电话号码的字母组合 (letterCombinations)
|
||||
@ -19,7 +20,36 @@ import java.util.List;
|
||||
* 链接:https://leetcode.cn/problems/letter-combinations-of-a-phone-number/
|
||||
*/
|
||||
//重做一遍
|
||||
//二刷会做
|
||||
public class LetterCombinations {
|
||||
void dfs(List<String>res,StringBuilder sb,int index,List<String>cur){
|
||||
if (index == cur.size()) {
|
||||
res.add(sb.toString());
|
||||
return;
|
||||
}
|
||||
String curString= cur.get(index);
|
||||
for (int i = 0; i < curString.length(); i++) {
|
||||
sb.append(curString.charAt(i));
|
||||
dfs(res,sb,index+1,cur);
|
||||
sb.deleteCharAt(sb.length()-1);
|
||||
}
|
||||
}
|
||||
public List<String> letterCombinations2(String digits) {
|
||||
String[] dict={
|
||||
"abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"
|
||||
};
|
||||
List<String>cur=new ArrayList<>();
|
||||
List<String> res=new ArrayList<>();
|
||||
if(Objects.equals(digits, ""))return new ArrayList<>();
|
||||
for (int i = 0; i < digits.length(); i++) {
|
||||
cur.add(dict[digits.charAt(i)-'2']);
|
||||
}
|
||||
StringBuilder sb=new StringBuilder();
|
||||
dfs(res,sb,0,cur);
|
||||
return res;
|
||||
}
|
||||
|
||||
|
||||
public List<String> letterCombinations(String digits) {
|
||||
List<String> res = new ArrayList<>();
|
||||
// 如果输入为空,直接返回空结果
|
||||
|
@ -18,11 +18,12 @@ import java.util.List;
|
||||
* 链接:https://leetcode.cn/problems/palindrome-partitioning/
|
||||
*/
|
||||
//不会
|
||||
//二刷不会
|
||||
public class Partition {
|
||||
|
||||
/**
|
||||
* 递归与回溯:
|
||||
* 从字符串的起始位置开始,遍历所有可能的分割点,对于每个可能的子串,先判断它是否为回文串。
|
||||
*
|
||||
* 如果是回文串,则将其加入当前的分割方案,然后递归处理剩下的部分。
|
||||
* 当走到字符串末尾时,说明当前方案已经完成,将该方案加入最终结果。
|
||||
* 回溯时需要将上一步加入的子串移除,以便尝试其他分割方案。
|
||||
|
@ -16,7 +16,24 @@ import java.util.List;
|
||||
* 链接:https://leetcode.cn/problems/subsets-ii/
|
||||
*/
|
||||
//不会,有点难
|
||||
//二刷会做,简单
|
||||
public class SubsetsWithDup {
|
||||
void dfs(List<List<Integer>> res,List<Integer> path,int[] nums ,int begin){
|
||||
res.add(new ArrayList<>(path));
|
||||
for (int i = begin; i < nums.length; i++) {
|
||||
if(i>begin&&nums[i]==nums[i-1])continue;
|
||||
path.add(nums[i]);
|
||||
dfs(res,path,nums,i+1);
|
||||
path.remove(path.size()-1);
|
||||
}
|
||||
}
|
||||
public List<List<Integer>> subsetsWithDup2(int[] nums) {
|
||||
Arrays.sort(nums);
|
||||
List<List<Integer>> res=new ArrayList<>();
|
||||
List<Integer> path=new ArrayList<>();
|
||||
dfs(res,path,nums,0);
|
||||
return res;
|
||||
}
|
||||
|
||||
public List<List<Integer>> subsetsWithDup(int[] nums) {
|
||||
List<List<Integer>> res = new ArrayList<>();
|
||||
|
15
src/test/java/backtrack/CombinationSum3Test.java
Normal file
15
src/test/java/backtrack/CombinationSum3Test.java
Normal file
@ -0,0 +1,15 @@
|
||||
package backtrack;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class CombinationSum3Test extends TestCase {
|
||||
|
||||
public void testCombinationSum33() {
|
||||
int k = 3, n = 7;
|
||||
CombinationSum3 solution = new CombinationSum3();
|
||||
List<List<Integer>> lists = solution.combinationSum33(k, n);
|
||||
System.out.println(lists.toString());
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user