Skip to content

Commit cc8e3f7

Browse files
author
haoyang.shi
committed
fix
1 parent 5503113 commit cc8e3f7

3 files changed

Lines changed: 23 additions & 50 deletions

File tree

offer/reConstructBinaryTree.md

Lines changed: 10 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -21,33 +21,19 @@ public TreeNode reConstructBinaryTree(int[] pre, int[] in) {
2121
return buildTree(preIndex, in, 0, in.length - 1);
2222
}
2323
24-
private TreeNode buildTree(Map<Integer, Integer> preIndex,
25-
int[] in, int start, int end) {
26-
Integer newRootValue = -1;
27-
Integer newRootValueIndexInPre = Integer.MAX_VALUE;
28-
for (int i = start; i <= end; i++) {
29-
Integer cur = preIndex.get(in[i]);
30-
if (cur < newRootValueIndexInPre) {
31-
newRootValueIndexInPre = cur;
32-
newRootValue = in[i];
33-
}
24+
private TreeNode buildTree(Map<Integer, Integer> preIndex, int[] in, int start, int end) {
25+
if (start == end) {
26+
return new TreeNode(in[start]);
3427
}
35-
36-
Integer newRootValueIndexInIn = 0;
37-
for (int i = 0; i < in.length; i++) {
38-
if (newRootValue == in[i]) {
39-
newRootValueIndexInIn = i;
28+
int indexOfRoot = start;
29+
for (int i = start; i <= end; i++) {
30+
if (preIndex.get(in[i]) < preIndex.get(in[indexOfRoot])) {
31+
indexOfRoot = i;
4032
}
4133
}
42-
43-
TreeNode root = new TreeNode(newRootValue);
44-
45-
if (start <= newRootValueIndexInIn - 1)
46-
root.left = buildTree(preIndex, in, start, newRootValueIndexInIn - 1);
47-
48-
if (newRootValueIndexInIn + 1 <= end)
49-
root.right = buildTree(preIndex, in, newRootValueIndexInIn + 1, end);
50-
34+
TreeNode root = new TreeNode(in[indexOfRoot]);
35+
if (start <= indexOfRoot - 1) root.left = buildTree(preIndex, in, start, indexOfRoot - 1);
36+
if (indexOfRoot + 1 <= end) root.right = buildTree(preIndex, in, indexOfRoot + 1, end);
5137
return root;
5238
}
5339
```

offer/replay-space.md

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,24 +14,11 @@
1414
```
1515
public String replaceSpace(StringBuffer str) {
1616
char[] chars = str.toString().toCharArray();
17-
int spaceCount = 0;
18-
17+
StringBuilder res = new StringBuilder();
1918
for (char c : chars) {
20-
if (c == ' ') spaceCount++;
19+
if (c == ' ') res.append("%20");
20+
else res.append(c);
2121
}
22-
23-
char[] res = new char[chars.length + spaceCount * 2];
24-
25-
for (int i = 0, j = 0; i < chars.length; i++) {
26-
if (chars[i] == ' ') {
27-
res[j++] = '%';
28-
res[j++] = '2';
29-
res[j++] = '0';
30-
} else {
31-
res[j++] = chars[i];
32-
}
33-
}
34-
35-
return new String(res);
22+
return res.toString();
3623
}
3724
```

offer/two-stack-fifo.md

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
## 题目
44

5-
https://www.nowcoder.com/practice/54275ddae22f475981afa2244dd448c6?tpId=13&tqId=11158&rp=1&ru=/ta/coding-interviews&qru=/ta/coding-interviews/question-ranking
5+
[牛客网](https://www.nowcoder.com/practice/54275ddae22f475981afa2244dd448c6?tpId=13&tqId=11158&rp=1&ru=/ta/coding-interviews&qru=/ta/coding-interviews/question-ranking)
66

77
用两个栈来实现一个队列,完成队列的 Push 和 Pop 操作。 队列中的元素为int类型。
88

@@ -15,20 +15,20 @@ https://www.nowcoder.com/practice/54275ddae22f475981afa2244dd448c6?tpId=13&tqId=
1515
相当于将两个 stack 拼接:-> stack1 <::> stack2 ->
1616

1717
```
18-
Stack<Integer> stack1 = new Stack<>();
19-
Stack<Integer> stack2 = new Stack<>();
18+
Stack<Integer> pushStack = new Stack<>();
19+
Stack<Integer> popStack = new Stack<>();
2020
2121
public void push(int node) {
22-
stack1.push(node);
22+
pushStack.push(node);
2323
}
2424
2525
public int pop() {
26-
if (stack2.isEmpty()) {
27-
while (!stack1.isEmpty()) {
28-
stack2.push(stack1.pop());
26+
if (popStack.isEmpty()) {
27+
while (!pushStack.isEmpty()) {
28+
popStack.push(pushStack.pop());
2929
}
3030
}
31-
32-
return stack2.pop();
31+
if (popStack.isEmpty()) return -1;
32+
else return popStack.pop();
3333
}
3434
```

0 commit comments

Comments
 (0)