提交 f6df74a7 编写于 作者: L liu13

20190315

上级 dc318574
package code;
import java.util.ArrayList;
import java.util.List;
/*
* 113. Path Sum II
* 题意:找从root到叶子节点和为sum的路径
* 难度:Medium
* 分类:Tree, Depth-first Search
* 思路:回溯,注意因为节点上可能正值,可能负值,所以不能剪枝
* Tips:
*/
public class lc113 {
public class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) {
val = x;
}
}
public List<List<Integer>> pathSum(TreeNode root, int sum) {
List<List<Integer>> res = new ArrayList<>();
helper(res, root, sum, 0, new ArrayList<>());
return res;
}
public void helper(List<List<Integer>> res, TreeNode root, int sum, int curr, List<Integer> curr_ls) {
if(root==null) return;
curr_ls.add(root.val);
if(curr+root.val==sum && root.left==null && root.right==null) { //到叶子节点
res.add(new ArrayList<>(curr_ls));
curr_ls.remove(curr_ls.size()-1);
return;
}
helper(res, root.left, sum, curr+root.val, curr_ls);
helper(res, root.right, sum, curr+root.val, curr_ls);
curr_ls.remove(curr_ls.size()-1);
}
}
......@@ -28,7 +28,7 @@ public class lc31 {
//从这个数之后的数中找出第一个比x大的数
int n = nums[ptr];
int ptr2 = ptr;
for(int i=ptr+1; i<nums.length; i++){
for(int i=ptr+1; i<nums.length; i++){ //这不用这么麻烦,后边的数有序的,这可以简化
if( nums[i]>nums[ptr-1] && nums[i]<=n ) {//注意 <= {2,3,1,3,3}
n = nums[i];
ptr2 = i;
......
......@@ -14,7 +14,7 @@ public class lc49 {
HashMap<String,List<String>> m = new HashMap();
for (int i = 0; i < strs.length ; i++) {
char[] chs = strs[i].toCharArray();
Arrays.sort(chs);
Arrays.sort(chs); //对字符串排序
String key = String.valueOf(chs);
if(m.containsKey(key))
m.get(key).add(strs[i]);
......
......@@ -89,15 +89,18 @@ LeetCode 指南
| 095 [Java](./code/lc95.java)
| 096 [Java](./code/lc96.java)
| 098 [Java](./code/lc98.java)
| 100 [Java](./code/lc100.java)
| 101 [Java](./code/lc101.java)
| 102 [Java](./code/lc102.java)
| 103 [Java](./code/lc103.java)
| 104 [Java](./code/lc104.java)
| 105 [Java](./code/lc105.java)
| 108 [Java](./code/lc108.java)
| 113 [Java](./code/lc113.java)
| 114 [Java](./code/lc114.java)
| 116 [Java](./code/lc116.java)
| 118 [Java](./code/lc118.java)
| 119 [Java](./code/lc119.java)
| 120 [Java](./code/lc120.java)
| 121 [Java](./code/lc121.java)
| 122 [Java](./code/lc122.java)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册