Skip to content

Commit ba976d6

Browse files
committed
fd
1 parent 6324c1c commit ba976d6

File tree

8 files changed

+112
-48
lines changed

8 files changed

+112
-48
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@
138138
|126|[Word Ladder II](https://leetcode.com/problems/word-ladder-ii/)| [Java](leetcode/solution/src/WordLadderII.java)||
139139
|127|[Word Ladder](https://leetcode.com/problems/word-ladder/)| [Java](leetcode/solution/src/WordLadder.java)|70|此题非常经典,务必连同ii多做几遍,将双端BFS吃透|
140140
|128|[Longest Consecutive Sequence](https://leetcode.com/problems/longest-consecutive-sequence/)| [Java](leetcode/solution/src/LongestConsecutiveSequence.java)|60|
141-
|129|[Sum Root to Leaf Numbers](https://leetcode.com/problems/sum-root-to-leaf-numbers/)| [Java](leetcode/solution/src/SumRootToLeafNumbers.java)|60|这题错了几次,多做几遍|
141+
|129|[Sum Root to Leaf Numbers](https://leetcode.com/problems/sum-root-to-leaf-numbers/)| [Java](leetcode/solution/src/SumRootToLeafNumbers.java)|100||
142142
|130|[Surrounded Regions](https://leetcode.com/problems/surrounded-regions/)| [Java](leetcode/solution/src/SurroundedRegions.java)|65|
143143
|131|[Palindrome Partitioning](https://leetcode.com/problems/palindrome-partitioning/)| [Java](leetcode/solution/src/PalindromePartitioning.java)||
144144
|132|[Palindrome Partitioning II](https://leetcode.com/problems/palindrome-partitioning-ii/)| [Java](leetcode/solution/src/PalindromePartitioningII.java)||

leetcode/common/src/Utils.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
public class Utils {
2+
3+
public static int max(int... val) {
4+
int max = Integer.MIN_VALUE;
5+
for (int k : val) {
6+
max = Math.max(max, k);
7+
}
8+
return max;
9+
}
10+
}

leetcode/solution/src/InorderSuccessorInBST.java

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,10 @@ public TreeNode inorderSuccessor(TreeNode root, TreeNode p) {
3030
}
3131

3232
/**
33-
* p的下一个节点一定是比p大的,所以这里遍历时当p的值小于当前节点,则当前节点
34-
* 可作为备选,同时往左走。如果在遍历过程中遇到仍然比p大的,说明更接近p,则更新备选。
35-
*
33+
* p的下一个节点一定是比p大的,所以如果p没有右子树,则结果是p的父节点,如果有右子树则是右子树中最小的节点。
34+
* 所以这里遍历时当p的值小于当前节点,则当前节点可作为备选,同时往左走。如果在遍历过程中遇到仍然比p大的,说明更接近p,则更新备选。
35+
* 如果遇到比p小的,就往右走
36+
* <p>
3637
* 有两点要注意,
3738
* 1, 首先res初始要为null,一个节点时,或root为null时,或p为最大节点时,res都没机会赋值
3839
* 2, 当root迭代到等于p时,走哪个分支呢,为什么选root = root.right,假如root.right为空,则之前的res即可,否则
@@ -52,11 +53,39 @@ public TreeNode inorderSuccessor2(TreeNode root, TreeNode p) {
5253
return res;
5354
}
5455

56+
// 耗时2ms,简单的递归写法
57+
/*
58+
public TreeNode inorderSuccessor(TreeNode root, TreeNode p) {
59+
if (root == null) {
60+
return null;
61+
}
62+
if (root.val > p.val) {
63+
TreeNode node = inorderSuccessor(root.left, p);
64+
return node != null ? node : root;
65+
} else {
66+
return inorderSuccessor(root.right, p);
67+
}
68+
}
69+
70+
public TreeNode predecessor(TreeNode root, TreeNode p) {
71+
if (root == null)
72+
return null;
73+
74+
if (root.val >= p.val) {
75+
return predecessor(root.left, p);
76+
} else {
77+
TreeNode right = predecessor(root.right, p);
78+
return (right != null) ? right : root;
79+
}
80+
}*/
81+
5582
/**
5683
* http://www.geeksforgeeks.org/?p=9999
5784
* 给定Node,求其successor,步骤如下:
5885
* 1, 如果Node.right != null,则在Node.right中找最小的那个节点,即从Node.right开始,最左下角的节点
5986
* 2, 如果Node.right == null,则不断往parent走,直到当前节点是其parent的左节点为止,其parent即为给定Node的successor
87+
*/
88+
/*
6089
private TreeNode inOrderSuccessor(TreeNode root, TreeNode node) {
6190
if (node.right != null) {
6291
return minValue(node.right);

leetcode/solution/src/PathSumII.java

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,30 @@
1+
import java.util.ArrayList;
12
import java.util.LinkedList;
23
import java.util.List;
34

45
public class PathSumII {
56

67
public List<List<Integer>> pathSum(TreeNode root, int sum) {
78
List<List<Integer>> result = new LinkedList<>();
8-
pathSum(root, sum, result, new LinkedList<Integer>());
9+
helper(root, sum, result, new LinkedList<>());
910
return result;
1011
}
1112

12-
/**
13-
* 这里一定要拷贝一份链表再加到result
14-
* 此时path中已经包含了root,sum中还不包含root
15-
*/
16-
private void pathSum(TreeNode root, int sum, List<List<Integer>> result, List<Integer> list) {
17-
if (root == null) {
13+
private void helper(TreeNode node, int sum, List<List<Integer>> result, List<Integer> list) {
14+
if (node == null) {
1815
return;
1916
}
2017

21-
list.add(root.val);
18+
list.add(node.val);
19+
sum -= node.val;
2220

23-
if (root.left == null && root.right == null && sum == root.val) {
24-
result.add(new LinkedList<>(list));
25-
return;
26-
}
27-
28-
if (root.left != null) {
29-
pathSum(root.left, sum - root.val, result, list);
30-
list.remove(list.size() - 1);
21+
if (node.left == null && node.right == null && sum == 0) {
22+
result.add(new ArrayList<>(list));
23+
} else {
24+
helper(node.left, sum, result, list);
25+
helper(node.right, sum, result, list);
3126
}
3227

33-
if (root.right != null) {
34-
pathSum(root.right, sum - root.val, result, list);
35-
list.remove(list.size() - 1);
36-
}
28+
list.remove(list.size() - 1);
3729
}
3830
}
Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,24 @@
11
public class SumRootToLeafNumbers {
22

3-
private int result;
4-
53
public int sumNumbers(TreeNode root) {
6-
sumNumbers(root, 0);
7-
return result;
4+
int[] res = new int[1];
5+
helper(root, 0, res);
6+
return res[0];
87
}
98

10-
private void sumNumbers(TreeNode root, int sum) {
11-
if (root == null) {
9+
private void helper(TreeNode node, int sum, int[] res) {
10+
if (node == null) {
1211
return;
1312
}
1413

15-
sum = sum * 10 + root.val;
14+
int n = sum * 10 + node.val;
1615

17-
if (root.left == null && root.right == null) {
18-
result += sum;
19-
return;
16+
if (node.left == null && node.right == null) {
17+
res[0] += n;
18+
} else {
19+
helper(node.left, n, res);
20+
helper(node.right, n, res);
2021
}
21-
22-
sumNumbers(root.left, sum);
23-
sumNumbers(root.right, sum);
2422
}
23+
2524
}

leetcode/solution/src/TwoSum.java

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ public class TwoSum {
77

88
/**
99
* 如果符合条件的不止一组呢?则找到一组就从map删除一组
10-
* @return
10+
* 要注意排除index != i
1111
*/
1212
public int[] twoSum(int[] nums, int target) {
1313
HashMap<Integer, Integer> map = new HashMap<>();
@@ -24,4 +24,20 @@ public int[] twoSum(int[] nums, int target) {
2424
}
2525
return null;
2626
}
27+
28+
/**
29+
* 只循环一轮
30+
* 要注意map.put要放在for末尾,对于case[3, 3], target=6的情况,如果放在开头会覆盖第一个3
31+
*/
32+
public int[] twoSum2(int[] nums, int target) {
33+
HashMap<Integer, Integer> map = new HashMap<>();
34+
for (int i = 0; i < nums.length; i++) {
35+
int k = map.getOrDefault(target - nums[i], -1);
36+
if (k >= 0 && k != i) {
37+
return new int[] {i, k};
38+
}
39+
map.put(nums[i], i);
40+
}
41+
return null;
42+
}
2743
}

leetcode/solution/src/TwoSumIII.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
/**
44
* 支持相同数存在
5+
* 如果需要find快,则每次add时都要遍历map中所有key,更新sum的map
56
*/
67
public class TwoSumIII {
78

leetcode/src/Main.java

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,38 @@
1-
import sun.plugin2.os.windows.FLASHWINFO;
2-
31
import java.util.*;
42

53
public class Main {
64

7-
public String tree2str(TreeNode t) {
8-
if (t == null) {
9-
return "";
5+
class TwoSum {
6+
7+
HashMap<Integer, Integer> map;
8+
9+
/** Initialize your data structure here. */
10+
public TwoSum() {
11+
map = new HashMap<>();
1012
}
11-
if (t.left == null && t.right == null) {
12-
return t.val + "";
13+
14+
/** Add the number to an internal data structure.. */
15+
public void add(int number) {
16+
map.put(number, map.getOrDefault(number, 0) + 1);
17+
}
18+
19+
/** Find if there exists any pair of numbers which sum is equal to the value. */
20+
public boolean find(int value) {
21+
for (int k : map.keySet()) {
22+
int n = map.getOrDefault(value - k, -1);
23+
if (value - k != k && n > 0) {
24+
return true;
25+
}
26+
if (value - k == k && n > 1) {
27+
return true;
28+
}
29+
}
30+
return false;
1331
}
14-
String left = "(" + tree2str(t.left) + ")";
15-
String right = t.right != null ? "(" + tree2str(t.right) + ")" : "";
16-
return t.val + left + right;
1732
}
1833

1934
public static void main(String[] args) {
35+
int[] res = twoSum(new int[]{3,2,3}, 6);
36+
System.out.println(String.format("%d - %d", res[0], res[1]));
2037
}
2138
}

0 commit comments

Comments
 (0)