![](https://img-blog.csdnimg.cn/20201014180756913.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
Depth First Search
USC_ECE
这个作者很懒,什么都没留下…
展开
-
JZ27 字符串的排列
https://leetcode-cn.com/problems/zi-fu-chuan-de-pai-lie-lcof/solution/mian-shi-ti-38-zi-fu-chuan-de-pai-lie-hui-su-fa-by/ 代码如下: import java.util.*; public class Solution { public ArrayList<String> Permutation(String str) { ArrayList<原创 2020-09-06 23:14:09 · 125 阅读 · 0 评论 -
JZ24 二叉树中和为某一值的路径
https://leetcode-cn.com/problems/er-cha-shu-zhong-he-wei-mou-yi-zhi-de-lu-jing-lcof/solution/mian-shi-ti-34-er-cha-shu-zhong-he-wei-mou-yi-zh-5/ 代码如下: import java.util.ArrayList; /** public class TreeNode { int val = 0; TreeNode left = null;原创 2020-08-31 23:54:56 · 175 阅读 · 0 评论 -
LeetCode 437. Path Sum III
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode() {} * TreeNode(int val) { this.val = val; } * TreeNode(int val, TreeNode left, TreeNode right) { .原创 2020-08-11 10:59:36 · 72 阅读 · 0 评论 -
LeetCode 113. Path Sum II
这道题用DFS解比较好,注意吃和吐成对出现,代码如下: /** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode() {} * TreeNode(int val) { this.val = val; } * TreeNode(int val, TreeN原创 2020-08-11 08:27:04 · 81 阅读 · 0 评论 -
LeetCode 51. N-Queens
class Solution { public List<List<String>> solveNQueens(int n) { char[][] chess = new char[n][n]; for(int i = 0; i < n; i++){ for(int j = 0; j < n; j++){ chess[i][j] = '.'; } .原创 2020-08-04 09:42:27 · 69 阅读 · 0 评论 -
LeetCode 254. Factor Combinations
step 1: 先按照从大到小的顺序找出所有的因数 step 2: 递归地解决问题(类似于99 cents) 需要注意的是在尝试完本层所有的branch以后退回上一层之前需要状态重置即删除之前加入path的数。 class Solution { public List<List<Integer>> getFactors(int n) { List<List<Integer>> res = new ArrayList<>原创 2020-08-03 09:37:45 · 95 阅读 · 0 评论 -
LaiCode 179. All Valid Permutations Of Parentheses II
public class Solution { public List<String> validParentheses(int l, int m, int n) { // Write your solution here int targetLen = (l + m + n) * 2; StringBuilder curr = new StringBuilder(); List<String> res = new ArrayList<&g.原创 2020-08-02 08:17:10 · 228 阅读 · 0 评论 -
LeetCode 79. Word Search
class Solution { public boolean exist(char[][] board, String word) { // Corner Case if(board == null || board.length == 0 || board[0] == null || board[0].length == 0){ return false; } int rows = board.length.原创 2020-07-30 09:45:35 · 78 阅读 · 0 评论 -
LeetCode 17. Letter Combinations of a Phone Number
先特判,再创建一个String数组来存储数字和字母之间的对应关系,剩下的就是经典的DFS了。 class Solution { public List<String> letterCombinations(String digits) { List<String> res = new ArrayList<>(); if(digits == null || digits.length() == 0){ re原创 2020-07-30 08:26:15 · 94 阅读 · 0 评论 -
LeetCode 91. Decode Ways
Approach 1 Recursion class Solution { public int numDecodings(String s) { return helper(s, 0); } private int helper(String s, int start){ if(start == s.length()){ return 1; } if(s.charAt(start) == '原创 2020-07-19 13:00:02 · 89 阅读 · 0 评论 -
LeetCode 216. Combination Sum III
class Solution { public List<List<Integer>> combinationSum3(int k, int n) { List<List<Integer>> res = new ArrayList<>(); backtrack(k, n, 1, new ArrayList<>(), res); return res; } priv.原创 2020-07-06 10:08:52 · 73 阅读 · 0 评论 -
LeetCode 90. Subsets II
Approach 1 如果当前的数字等于path的最后一个数字,那么我们就不考虑不添加这个数的情况了。注意条件是path.size() == 0 而不是 index == 0, 这是因为index != 0时仍然存在path.size() == 0的情况,导致path.size() - 1 < 0,path.get()方法报错。 class Solution { public List<List<Integer>> subsetsWithDup(int[] num原创 2020-07-06 08:29:39 · 136 阅读 · 0 评论 -
LeetCode 47. Permutations II
https://leetcode-cn.com/problems/permutations-ii/solution/hui-su-suan-fa-python-dai-ma-java-dai-ma-by-liwe-2/ 对于这个解法,我还有另一个视角的理解。在nums排序以后,考虑几个相同的数,如果排在某个数前面的数还未被使用(used[i - 1] == false),那么我们不得使用该数。其实这里所做的工作就是强行给这几个相同的数做了出现次序规定:一定要前面的加入到path以后,才能再添加后面的数。所原创 2020-07-04 08:41:43 · 89 阅读 · 0 评论 -
LeetCode 46. Permutations
https://leetcode-cn.com/problems/permutations/solution/hui-su-suan-fa-xiang-jie-by-labuladong-2/ 这里值得注意的是List有contains()方法可用。 class Solution { public List<List<Integer>> permute(int[] nums) { List<List<Integer>> res =原创 2020-07-04 07:08:55 · 76 阅读 · 0 评论 -
LeetCode 40. Combination Sum II
https://leetcode-cn.com/problems/combination-sum-ii/solution/hui-su-suan-fa-jian-zhi-python-dai-ma-java-dai-m-3/ class Solution { public List<List<Integer>> combinationSum2(int[] candidates, int target) { List<List<Integer&g原创 2020-07-04 06:46:51 · 70 阅读 · 0 评论 -
LeetCode 77. Combinations
class Solution { public List<List<Integer>> combine(int n, int k) { List<List<Integer>> res = new ArrayList<>(); backtrack(n, k, 1, new ArrayList<>(), res); return res; } private void.原创 2020-07-04 05:33:59 · 72 阅读 · 0 评论 -
LeetCode 39. Combination Sum
class Solution { public List<List<Integer>> combinationSum(int[] candidates, int target) { List<List<Integer>> res = new ArrayList<>(); backtrack(candidates, target, 0, new ArrayList<>(), res); .原创 2020-07-03 08:47:08 · 67 阅读 · 0 评论 -
LeetCode 22. Generate Parentheses
StringBuilder类的toString()方法返回一个新的String对象,且对StringBuilder后续的修改不会影响该String。所以不用像List一样拷贝一份。代码如下: class Solution { public List<String> generateParenthesis(int n) { List<String> ans = new ArrayList<>(); DFS(n, 0, 0, new原创 2020-06-30 14:09:24 · 64 阅读 · 0 评论 -
LeetCode 78. Subsets
class Solution { public List<List<Integer>> subsets(int[] nums) { List<List<Integer>> ans = new ArrayList<>(); dfs(nums, 0, new ArrayList<Integer>(), ans); return ans; } private void .原创 2020-06-30 13:34:33 · 89 阅读 · 0 评论