Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 2d9f477

Browse files
committed
20190320
1 parent de13f7e commit 2d9f477

22 files changed

+116
-53
lines changed

‎code/lc112.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package code;
2+
/*
3+
* 112. Path Sum
4+
* 题意:是否存在跟到叶子节点的和为sum
5+
* 难度:Easy
6+
* 分类:
7+
* 思路:
8+
* Tips:lc112, lc113, lc437, lc129, lc124, lc337
9+
* 总结一下 lc112 找跟到叶子和为sum的路径是否存在
10+
* lc113 把lc112的路径找出来
11+
* lc437 是任意一条向下走的路径
12+
* lc129 计算所有的和
13+
* lc124 任意一条路径
14+
* lc337 树的dp
15+
*/
16+
public class lc112 {
17+
public class TreeNode {
18+
int val;
19+
TreeNode left;
20+
TreeNode right;
21+
TreeNode(int x) {
22+
val = x;
23+
}
24+
}
25+
public boolean hasPathSum(TreeNode root, int sum) {
26+
if(root==null) return false;
27+
if(root.val==sum&&root.left==null&&root.right==null) return true;
28+
return hasPathSum(root.left, sum-root.val)||hasPathSum(root.right, sum-root.val);
29+
}
30+
}

‎code/lc113.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
* 难度:Medium
99
* 分类:Tree, Depth-first Search
1010
* 思路:回溯,注意因为节点上可能正值,可能负值,所以不能剪枝
11-
* Tips:lc124
11+
* Tips:lc112, lc113, lc437, lc129, lc124, lc337
1212
*/
1313
public class lc113 {
1414
public class TreeNode {

‎code/lc124.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* 分类:Tree, Depth-first Search
77
* 思路:因为二叉树只有两个节点,一条路径可以想象成倒V字,从低层的某个节点一路向上,到达一个顶点,再一路向下,理解了这一点,整道题就好解了。
88
* Tips:用了一个全局变量存储最后结果,因为函数返回的是直线路径上的最优解,而不是V字路径最优解
9-
* lc133
9+
* lc112, lc113, lc437, lc129, lc124, lc337, lc543
1010
*/
1111
public class lc124 {
1212
public class TreeNode {

‎code/lc129.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package code;
2+
/*
3+
* 129. Sum Root to Leaf Numbers
4+
* 题意:一条路径上的数字组成一个数字,求所有从跟到叶子的路径和
5+
* 难度:Medium
6+
* 分类:Tree, Depth-first Search
7+
* 思路:dfs
8+
* Tips:lc112, lc113, lc437, lc129, lc124, lc337
9+
*/
10+
public class lc129 {
11+
public class TreeNode {
12+
int val;
13+
TreeNode left;
14+
TreeNode right;
15+
TreeNode(int x) {
16+
val = x;
17+
}
18+
}
19+
public int sumNumbers(TreeNode root) {
20+
if(root==null) return 0;
21+
return helper(root, 0);
22+
}
23+
public int helper(TreeNode root, int sum){
24+
if(root==null) return 0; //一条路径,另一边返回0
25+
if(root.left==null&&root.right==null) return sum*10+root.val; //这点注意,是叶子节点直接返回了
26+
return helper(root.left, sum*10+root.val) + helper(root.right, sum*10+root.val);
27+
}
28+
}

‎code/lc151.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package code;
2+
3+
import java.util.Arrays;
4+
import java.util.Collections;
5+
/*
6+
* 151. Reverse Words in a String
7+
* 题意:反转字符串中的单词
8+
* 难度:Medium
9+
* 分类:String
10+
* 思路:难点在中间的空格可能是多个空格,注意如何解决
11+
* Tips:
12+
*/
13+
public class lc151 {
14+
public String reverseWords(String s) {
15+
String[] words = s.trim().split(" +"); //+号匹配多个
16+
Collections.reverse(Arrays.asList(words));
17+
return String.join(" ", words);
18+
}
19+
}

‎code/lc236.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public TreeNode lowestCommonAncestor2(TreeNode root, TreeNode p, TreeNode q) {
3535
parent.put(root, null);
3636
stack.push(root);
3737

38-
while (!parent.containsKey(p) || !parent.containsKey(q)) {
38+
while (!parent.containsKey(p) || !parent.containsKey(q)) {//遍历了一遍节点,把节点的父节点信息记录了一下
3939
TreeNode node = stack.pop();
4040
if (node.left != null) {
4141
parent.put(node.left, node);
@@ -47,11 +47,11 @@ public TreeNode lowestCommonAncestor2(TreeNode root, TreeNode p, TreeNode q) {
4747
}
4848
}
4949
Set<TreeNode> ancestors = new HashSet<>();
50-
while (p != null) {
50+
while (p != null) {//p的路径节点添加到hashset中
5151
ancestors.add(p);
5252
p = parent.get(p);
5353
}
54-
while (!ancestors.contains(q))
54+
while (!ancestors.contains(q))//第一个hashset中遇到的节点,就是最近公共祖先
5555
q = parent.get(q);
5656
return q;
5757
}

‎code/lc239.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
package code;
22
/*
33
* 239. Sliding Window Maximum
4-
* 题意:滑动窗口最大值
4+
* 题意:滑动窗口中最大值
55
* 难度:Hard
66
* 分类:Heap
7-
* 思路:用双向队列,保证队列里是递增的。单调队列,好好学习一下。
7+
* 思路:用双向队列,保证队列里是递减的。单调队列,好好学习一下。
88
* Tips:与lc84做比较,84是递增栈
99
*/
1010
import java.util.ArrayDeque;
@@ -25,7 +25,7 @@ public static int[] maxSlidingWindow(int[] nums, int k) {
2525
return new int[]{};
2626
int[] res = new int[nums.length-k+1];
2727
int cur = 0;
28-
Deque<Integer> dq = new ArrayDeque();
28+
Deque<Integer> dq = new ArrayDeque();//队列里是递减的
2929
for (int i = 0; i < nums.length ; i++) {
3030
if( !dq.isEmpty() && dq.peekFirst()<=i-k)
3131
dq.removeFirst();

‎code/lc268.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* 思路:两种巧妙的方法,时间空间都是O(1)
88
* 异或
99
* 求和以后,减去所有
10-
* Tips:
10+
* Tips:lc268 lc448 lc287
1111
*/
1212
public class lc268 {
1313
public int missingNumber(int[] nums) {
@@ -16,7 +16,7 @@ public int missingNumber(int[] nums) {
1616
return res;
1717
}
1818
public int missingNumber2(int[] nums) {
19-
int res = nums.length;
19+
int res = nums.length;//异或上长度
2020
for(int i=0; i<nums.length; i++) res^=i^nums[i];
2121
return res;
2222
}

‎code/lc279.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public static int numSquares(int n) {
1717
int[] dp = new int[n];
1818
Arrays.fill(dp,Integer.MAX_VALUE);
1919
for (int i = 1; i <= n ; i++) { //两个for循环
20-
for (int j=1; j<=i ; j++) {
20+
for (int j=1; j*j<=i ; j++) {
2121
if(j*j==i)
2222
dp[i-1] = 1;
2323
if(j*j<i){

‎code/lc287.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@
33
* 287. Find the Duplicate Number
44
* 题意:n+1个数属于[1~n],找出重复的那个数
55
* 难度:Medium
6-
* 分类:Array, Two Pointers, Binary Search
6+
* 分类:Array, Two Pointers, Binary Searn+1个数属于[1~n],找出重复的那个数ch
77
* 思路:如果nums[i]不在对应位置,则和对应位置交换。如果对应位置上也为该数,说明这个数就是重复的数字。这个方法改变了数组。是错误的。
88
* 另一种方法,把问题转换成有环链表,找环的起始节点。O(n) O(1) lc142
99
* 二分查找,每次看一边数字的个数, O(nlog(n)) O(1)
1010
* Tips:剑指offer原题
11+
* lc268 lc448 lc287
1112
*/
1213
public class lc287 {
1314
public int findDuplicate(int[] nums) { //该方法修改了数组,是错误的,没看清题意

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /