回溯
软件园里卖煎饼
以文载意,以理铸心
展开
-
每日一题:518. 零钱兑换 II
解题思路记忆化搜索代码class Solution { Set<String> set = new HashSet<String>(); int[][] memo; public int change(int amount, int[] coins) { memo = new int[coins.length][amount+1];//记忆数组 Arrays.sort(coins); return back原创 2021-06-28 15:46:45 · 107 阅读 · 0 评论 -
每日任务:回溯.216. 组合总和 III
解题思路dfs加回溯,对着案例看代码代码class Solution { List<List<Integer>> ans; int[] num = {1,2,3,4,5,6,7,8,9};//数字只能从这里面来 public List<List<Integer>> combinationSum3(int k, int n) { ans = new ArrayList<>(); boole原创 2021-04-25 22:23:09 · 58 阅读 · 0 评论 -
每日任务:回溯.40. 组合总和 II
解题思路dfs加回溯,结合案例看代码就能明白,我不好讲代码class Solution { List<List<Integer>> ans; public List<List<Integer>> combinationSum2(int[] candidates, int target) { ans = new ArrayList<>(); LinkedList<Integer> te原创 2021-04-25 22:19:07 · 49 阅读 · 0 评论 -
每日任务:回溯.39. 组合总和
解题思路dfs加回溯,思路不好讲出来,可以结合案例看代码代码class Solution { List<List<Integer>> ans; public List<List<Integer>> combinationSum(int[] candidates, int target) { ans = new ArrayList<>(); LinkedList<Integer> te原创 2021-04-25 22:14:48 · 46 阅读 · 0 评论 -
每日任务:回溯.47. 全排列 II
解题思路bfs加回溯,需要判断是否有重复代码class Solution { List<List<Integer>> ans; public List<List<Integer>> permuteUnique(int[] nums) { ans = new ArrayList<>(); boolean[] visited = new boolean[nums.length]; L原创 2021-04-25 22:11:15 · 43 阅读 · 0 评论 -
每日任务:回溯.78. 子集
解题思路dfs回溯,easy~代码class Solution { List<List<Integer>> ans; public List<List<Integer>> subsets(int[] nums) { ans = new ArrayList<>(); LinkedList<Integer> tem = new LinkedList<>();原创 2021-04-25 22:07:45 · 51 阅读 · 0 评论 -
每日任务:回溯.77. 组合
解题思路从1开始不断增大,并进行组合代码class Solution { List<List<Integer>> ans; public List<List<Integer>> combine(int n, int k) { ans = new ArrayList<>(); LinkedList<Integer> tem = new LinkedList<>();原创 2021-04-25 22:04:49 · 44 阅读 · 0 评论 -
回溯法解题思路及模板
回溯法个人觉得最重要的两点:第一点.终止条件第二点.恢复操作如果能明白这两点,那么其他代码的编写可以说是大同小异了解决一个回溯问题,实际上就是一个决策树的遍历过程。1、路径:也就是已经做出的选择。2、选择列表:也就是你当前可以做的选择。3、结束条件:也就是到达决策树底层,无法再做选择的条件。标准的可以用回溯法解决的问题有全排列问题以及N皇后问题,能掌握这两道题目,基本对于回溯的理解就差不多了for 选择 in 选择列表: # 做选择 将该选择从选择列表移除 路径.原创 2021-04-12 14:36:50 · 553 阅读 · 0 评论 -
回溯法:LeetCode:51. N 皇后问题
解题思路最开始写理解错了,注意!!!(N皇后要求的是所在的列和行,以及斜方向上都不存在其他皇后)我们需要将皇后一层一层的探索尝试,判断其是否在当前位置上能够放置并且在一次完成后需要对现场进行恢复如果觉得理解起来有点困难的话,可以尝试着先对皇后排列情况进行穷举,不考虑其要求,然后在这样的基础上,再去进行修改,相信打架就都能理解并且掌握这道题目了代码class Solution { List<List<String>> ans; String[][] map;原创 2021-04-12 14:26:33 · 114 阅读 · 2 评论 -
回溯法:LeetCode:46. 全排列
解题思路回溯法,YYDS基本思想是暴力穷举,但是精髓在于当一个情况符合要求后,需要进行恢复的操作,在这道题中,比如1->2->3这条路走过了,那么我们要将路线变成1->2,再变成1,这样才能继续走1->3这一条路径代码class Solution { List<List<Integer>> ans; public List<List<Integer>> permute(int[] nums) {原创 2021-04-12 14:19:40 · 68 阅读 · 0 评论