Skip to content

Commit 2d9f477

Browse files
committed
20190320
1 parent de13f7e commit 2d9f477

22 files changed

+116
-53
lines changed

Diff for: code/lc112.java

+30
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+
}

Diff for: code/lc113.java

+1-1
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 {

Diff for: code/lc124.java

+1-1
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 {

Diff for: code/lc129.java

+28
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+
}

Diff for: code/lc151.java

+19
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+
}

Diff for: code/lc236.java

+3-3
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
}

Diff for: code/lc239.java

+3-3
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();

Diff for: code/lc268.java

+2-2
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
}

Diff for: code/lc279.java

+1-1
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){

Diff for: code/lc287.java

+2-1
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) { //该方法修改了数组,是错误的,没看清题意

Diff for: code/lc303.java

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
* 分类:Dynamic Programming
77
* 思路:
88
* Tips:Bingo!
9+
* lc303, lc437, lc560
910
*/
1011
public class lc303 {
1112
class NumArray {

Diff for: code/lc337.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
* Tips:https://leetcode.com/problems/house-robber-iii/discuss/79330/Step-by-step-tackling-of-the-problem
1212
* 解答给了三种方法,递归,记忆递归,dp
1313
* 多理解一下树的dp,核心是返回数组而不是一个数字
14+
* lc112, lc113, lc437, lc129, lc124, lc337
1415
*/
1516
public class lc337 {
1617
public class TreeNode {
@@ -26,7 +27,7 @@ public int rob(TreeNode root) {
2627
public int helper(TreeNode root, HashMap<TreeNode, Integer> mem){
2728
if(root==null)
2829
return 0;
29-
if(mem.containsKey(root)) //用mem去记忆一下资情况的结果,防止重复计算
30+
if(mem.containsKey(root)) //用mem去记忆一下子情况的结果,防止重复计算
3031
return mem.get(root);
3132
int val =0;
3233
if(root.left!=null){

Diff for: code/lc378.java

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
* 思路:两种思路。 1是类似多个有序链表合并的思路,优先队列。
1212
* 2是二分,二分的是val,看比这个val小的数是不是k
1313
* Tips:lc23方法很像
14+
* lc240
1415
*/
1516
public class lc378 {
1617
class Cell{

Diff for: code/lc395.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public int longestSubstring(String s, int k) {
2323

2424
if( cur_uni_char==i && less_than_k_char==i) res = Math.max(res, right-left);
2525

26-
else if(cur_uni_char>i){ //左边推进
26+
else if(cur_uni_char>i){ //左边推进。不在外边加上一个循环的话,就不知道怎么推荐左指针了。
2727
while(cur_uni_char!=i){
2828
map[s.charAt(left)-'a']--;
2929
if(map[s.charAt(left)-'a']==0) cur_uni_char--;

Diff for: code/lc416.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
* 思路:题意可以转换为用任意个元素组成的和等于数组和/2。可以和 lc1, lc15 3-Sum 对比。
1010
* 0,1背包问题,递推比较简单,所以空间可以压缩成一维
1111
* 自己想的思路其实和压缩后的0,1背包类似,但没想到该问题可以抽象为0,1背包
12-
* Tips:
12+
* dp[i][j] = dp[i-1][j] || dp[i-1][j-nums[i]]
13+
* Tips:lc416, lc494
1314
*/
1415
public class lc416 {
1516
public static void main(String[] args) {

Diff for: code/lc437.java

+4-22
Original file line numberDiff line numberDiff line change
@@ -12,46 +12,28 @@
1212
* 递归的时候用减是否==0的方式,而不是+==sum的方式
1313
* 和lc560有共同的思想,每个节点只需遍历一遍就可以了
1414
* 虽然是Easy题,做好也不简单
15+
* lc112, lc113, lc437, lc129, lc124, lc337
16+
* lc303, lc437, lc560
1517
*/
1618
public class lc437 {
1719
public static class TreeNode {
1820
int val;
1921
TreeNode left;
2022
TreeNode right;
21-
2223
TreeNode(int x) {
2324
val = x;
2425
}
2526
}
2627

27-
public static void main(String[] args) {
28-
TreeNode node10 = new TreeNode(10);
29-
TreeNode node5 = new TreeNode(5);
30-
TreeNode node3 = new TreeNode(3);
31-
TreeNode node2 = new TreeNode(2);
32-
TreeNode node1 = new TreeNode(1);
33-
TreeNode noden3 = new TreeNode(-3);
34-
TreeNode node11 = new TreeNode(11);
35-
node10.left = node5;
36-
node10.right = noden3;
37-
node5.left = node3;
38-
node5.right = node2;
39-
node2.right = node1;
40-
noden3.right = node11;
41-
System.out.println(pathSum2(node10, 8));
42-
}
43-
4428

4529
public static int pathSum(TreeNode root, int sum) { //该节点作为起点
4630
if (root == null) return 0;
4731
return dfs(root, sum) + pathSum(root.left, sum) + pathSum(root.right, sum);
4832
}
4933

5034
public static int dfs(TreeNode root, int sum) { //一条路径向下走
51-
if (root == null)
52-
return 0;
53-
if (root.val == sum)
54-
return 1 + dfs(root.left, sum - root.val) + dfs(root.right, sum - root.val);//不要直接返回1,因为可能后边节点,或节点和为0
35+
if (root == null) return 0;
36+
if (root.val == sum) return 1 + dfs(root.left, sum - root.val) + dfs(root.right, sum - root.val);//不要直接返回1,因为可能后边节点,或节点和为0
5537
return dfs(root.left, sum - root.val) + dfs(root.right, sum - root.val);
5638
}
5739

Diff for: code/lc448.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
* 难度:Easy
1010
* 分类:Array
1111
* 思路:把对应的数字放到对应的位置,最后遍历一遍,如果位置和数字不对应,则为缺失的值。
12-
* Tips:
12+
* Tips:lc268 lc448 lc287
1313
*/
1414
public class lc448 {
1515
public static void main(String[] args) {

Diff for: code/lc494.java

+4-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@
55
* 难度:Medium
66
* 分类:Dynamic Programming, Depth-first Search
77
* 思路:可以用递归+mem的方法。也可以转化为0,1背包问题,注意dp的时候把下标移位。另一种方法是转化为子数组的和为(target + sum(nums))/2的问题,求解方法类似lc416
8-
* Tips:多抽象总结一下相关的问题,如何抽象出背包。对于这个数字,要么+,要么-,就两种情况。
8+
* Tips:多抽象总结一下相关的问题,如何抽象出背包。对于这个数字,要么+,要么-,就两种情况。https://leetcode.com/problems/target-sum/discuss/97335/Short-Java-DP-Solution-with-Explanation
9+
* dp[i][j] 表示前i个元素和为j的方案个数
10+
* dp[i][j] = dp[i-1][j-nums[j]] + dp[i-1][j+nums[j]] //加减两种方案加起来
11+
* lc416, lc494
912
*/
1013
public class lc494 {
1114
public int findTargetSumWays(int[] nums, int S) {

Diff for: code/lc538.java

+5-8
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,11 @@ public class TreeNode {
1717
}
1818
int sum = 0;
1919
public TreeNode convertBST(TreeNode root) {
20-
helper(root);
20+
if(root==null) return null;
21+
convertBST(root.right);
22+
sum += root.val;
23+
root.val = sum;
24+
convertBST(root.left);
2125
return root;
2226
}
23-
public void helper(TreeNode root){
24-
if(root==null) return;
25-
helper(root.right);
26-
root.val = root.val + sum;
27-
sum = root.val;
28-
helper(root.left);
29-
}
3027
}

Diff for: code/lc543.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package code;
22
/*
33
* 543. Diameter of Binary Tree
4-
* 题意:树种的最长路径
4+
* 题意:树中的最长路径
55
* 难度:Easy
66
* 分类:Tree
77
* 思路:和lc124思路一样,但lc124是Hard,这道竟然是Easy,哈哈哈

Diff for: code/lc560.java

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
* 分类:Array, Hash Table
1010
* 思路:求出累加和存在hashmap中,如果当前hashmap中存在sum-k,那么就是一个解
1111
* Tips:经典思路,记一下。lc437有类似思想。
12+
* lc303, lc437, lc560
1213
*/
1314
public class lc560 {
1415
public int subarraySum(int[] nums, int k) {

Diff for: code/lc617.java

+3-5
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,11 @@ public void helper(TreeNode t1, TreeNode t2){
4242
}
4343

4444
public TreeNode mergeTrees2(TreeNode t1, TreeNode t2) {
45-
if (t1 == null)
46-
return t2;
47-
if (t2 == null)
48-
return t1;
49-
t1.val += t2.val;
45+
if(t1==null) return t2; //这里注意一下,比较难想明白,可以记一下
46+
if(t2==null) return t1;
5047
t1.left = mergeTrees(t1.left, t2.left);
5148
t1.right = mergeTrees(t1.right, t2.right);
49+
t1.val += t2.val;
5250
return t1;
5351
}
5452
}

0 commit comments

Comments
 (0)