Skip to content

Commit d9a96f1

Browse files
committed
upate
1 parent fcc97ae commit d9a96f1

7 files changed

+47
-120
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -404,5 +404,6 @@
404404
|849|[Maximize Distance to Closest Person](https://leetcode.com/problems/maximize-distance-to-closest-person/)|[Java](leetcode/solution/src/MaximizeDistanceToClosestPerson.java)|80||
405405
|857|[Minimum Cost to Hire K Workers](https://leetcode.com/problems/minimum-cost-to-hire-k-workers/)|[Java](leetcode/solution/src/MinimumCostToHireKWorkers.java)|70||
406406
|872|[Leaf-Similar Trees](https://leetcode.com/problems/leaf-similar-trees/)|[Java](leetcode/solution/src/LeafSimilarTrees.java)|80||
407+
|889|[Construct Binary Tree from Preorder and Postorder Traversal](https://leetcode.com/problems/construct-binary-tree-from-preorder-and-postorder-traversal/)|[Java](leetcode/solution/src/ConstructBinaryTreeFromPreorderAndPostorderTraversal.java)|80||
407408
|904|[Fruit Into Baskets](https://leetcode.com/problems/fruit-into-baskets/)|[Java](leetcode/solution/src/FruitIntoBaskets.java)|90||
408409
|929|[Unique Email Addresses](https://leetcode.com/problems/unique-email-addresses/)|[Java](leetcode/solution/src/UniqueEmailAddresses.java)|90||

leetcode/solution/src/BinaryTreePostorderTraversal.java

Lines changed: 3 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -23,52 +23,12 @@ public List<Integer> postorderTraversal(TreeNode root) {
2323
return results;
2424
}
2525

26-
27-
public ArrayList<Integer> postorderTraversal2(TreeNode root) {
28-
ArrayList<Integer> result = new ArrayList<Integer>();
29-
Stack<TreeNode> stack = new Stack<TreeNode>();
30-
TreeNode prev = null; // previously traversed node
31-
TreeNode curr = root;
32-
33-
if (root == null) {
34-
return result;
35-
}
36-
37-
stack.push(root);
38-
while (!stack.empty()) {
39-
curr = stack.peek();
40-
if (prev == null || prev.left == curr || prev.right == curr) { // traverse down the tree
41-
if (curr.left != null) {
42-
stack.push(curr.left);
43-
} else if (curr.right != null) {
44-
stack.push(curr.right);
45-
}
46-
} else if (curr.left == prev) { // traverse up the tree from the left
47-
if (curr.right != null) {
48-
stack.push(curr.right);
49-
}
50-
} else { // traverse up the tree from the right
51-
result.add(curr.val);
52-
stack.pop();
53-
}
54-
prev = curr;
55-
}
56-
57-
return result;
58-
}
59-
60-
public List<Integer> postorderTraversal3(TreeNode root) {
26+
// root为null表示栈顶的左子树都访问完了,该看右子树了
27+
public List<Integer> postorderTraversal2(TreeNode root) {
6128
List<Integer> result = new ArrayList<>();
62-
63-
if (root == null) {
64-
return result;
65-
}
66-
6729
Stack<TreeNode> stack = new Stack<>();
6830

69-
TreeNode last = null;
70-
71-
while (!stack.isEmpty() || root != null) {
31+
for (TreeNode last = null; !stack.isEmpty() || root != null; ) {
7232
if (root != null) {
7333
stack.push(root);
7434
root = root.left;
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import java.util.HashMap;
2+
3+
public class ConstructBinaryTreeFromPreorderAndPostorderTraversal {
4+
5+
/**
6+
* 这里先记录一下post每个数对应的索引,免得每次去找
7+
* 不过找找也没关系,因为每个元素不会重复找
8+
*/
9+
public TreeNode constructFromPrePost(int[] pre, int[] post) {
10+
HashMap<Integer, Integer> map = new HashMap<>();
11+
for (int i = 0; i < post.length; i++) {
12+
map.put(post[i], i);
13+
}
14+
return helper(pre, 0, pre.length - 1, post, 0, post.length - 1, map);
15+
}
16+
17+
private TreeNode helper(int[] pre, int prestart, int preend, int[] post, int poststart, int postend, HashMap<Integer, Integer> map) {
18+
if (prestart > preend || poststart > postend) {
19+
return null;
20+
}
21+
TreeNode root = new TreeNode(pre[prestart]);
22+
if (prestart + 1 <= preend) {
23+
int len = map.get(pre[prestart + 1]) - poststart + 1;
24+
root.left = helper(pre, prestart + 1, prestart + len, post, poststart, poststart + len - 1, map);
25+
root.right = helper(pre, prestart + len + 1, preend, post, poststart + len, postend - 1, map);
26+
}
27+
return root;
28+
}
29+
}

leetcode/solution/src/FindMinimumInRotatedSortedArray.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
11
public class FindMinimumInRotatedSortedArray {
22

3+
/**
4+
* 由于[left, right]区间内包含了最小值,nums[left]通常是比nums[right]大的
5+
* 如果nums[left]<nums[right]只有一个可能,就是nums[left]最小
6+
*
7+
* TestCase
8+
* [1, 2]
9+
* [2, 1]
10+
* [1]
11+
*/
312
public int findMin(int[] nums) {
413
int left = 0, right = nums.length - 1;
514
while (left < right) {

leetcode/solution/src/SearchForARange.java

Lines changed: 1 addition & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ public int[] searchRange(int[] nums, int target) {
2121
};
2222
}
2323

24+
// 测区间只有1个数或两个数的情况
2425
private int firstHigherEqual(int[] nums, int target) {
2526
int left = 0, right = nums.length;
2627
while (left < right) {
@@ -33,52 +34,4 @@ private int firstHigherEqual(int[] nums, int target) {
3334
}
3435
return left;
3536
}
36-
37-
public int[] searchRange2(int[] nums, int target) {
38-
if (nums.length == 0) {
39-
return new int[]{-1, -1};
40-
}
41-
return new int[] {
42-
lowerBound(nums, target),
43-
upperBound(nums, target)
44-
};
45-
}
46-
47-
/**
48-
* lowerBound和upperBound的TestCase
49-
* [1,3],3
50-
* [1,3],1
51-
* [2,2],2
52-
*/
53-
public static int lowerBound(int[] nums, int target) {
54-
int left = 0, right = nums.length - 1;
55-
56-
while (left < right) {
57-
int mid = left + ((right - left) >> 1);
58-
59-
if (target > nums[mid]) {
60-
left = mid + 1;
61-
} else {
62-
right = mid;
63-
}
64-
}
65-
66-
return nums[left] == target ? left : -1;
67-
}
68-
69-
public static int upperBound(int[] nums, int target) {
70-
int left = 0, right = nums.length - 1;
71-
72-
while (left < right) {
73-
int mid = left + ((right - left) >> 1) + 1;
74-
75-
if (target < nums[mid]) {
76-
right = mid - 1;
77-
} else {
78-
left = mid;
79-
}
80-
}
81-
82-
return nums[right] == target ? right : -1;
83-
}
8437
}

leetcode/solution/src/SimplifyPath.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,6 @@ public class SimplifyPath {
55

66
public String simplifyPath(String path) {
77
String[] ss = path.split("/");
8-
/**
9-
* 注意这里要用双端队列,因为后面要生成路径需要从前往后
10-
*/
118
Deque<String> queue = new LinkedList<>();
129
for (String s : ss) {
1310
/**

leetcode/src/Main.java

Lines changed: 4 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -6,36 +6,14 @@
66
public class Main {
77

88
public static class Solution {
9-
public int lengthOfLongestSubstring(String s) {
10-
HashMap<Character, Integer> map = new HashMap<>();
11-
int max = 0;
12-
for (int i = 0, j = 0; i < s.length(); i++) {
13-
char c = s.charAt(i);
14-
map.put(c, map.getOrDefault(c, 0) + 1);
15-
for ( ; j < i && map.get(c) > 1; j++) {
16-
char cc = s.charAt(j);
17-
map.put(cc, map.get(cc) - 1);
18-
}
19-
max = Math.max(max, i - j + 1);
20-
}
21-
return max;
22-
}
239

24-
public int lengthOfLongestSubstring(String s) {
25-
int[] count = new int[256];
26-
int max = 0;
27-
for (int i = 0, j = 0; i < s.length(); i++) {
28-
char c = s.charAt(i);
29-
count[c]++;
30-
for ( ; j < i && count[c] > 1; j++) {
31-
count[s.charAt(j)]--;
32-
}
33-
max = Math.max(max, i - j + 1);
10+
public int myAtoi(String str) {
11+
str = str.trim();
12+
if (str.length() == 0) {
13+
return
3414
}
35-
return max;
3615
}
3716

38-
}
3917

4018

4119
public static void main(String[] args) {

0 commit comments

Comments
 (0)