Skip to content

Commit 08b278d

Browse files
committed
98
1 parent 0aebd4e commit 08b278d

5 files changed

+367
-13
lines changed

SUMMARY.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,4 +97,5 @@
9797
* [94. Binary Tree Inorder Traversal](leetCode-94-Binary-Tree-Inorder-Traversal.md)
9898
* [95. Unique Binary Search Trees II](leetCode-95-Unique-Binary-Search-TreesII.md)
9999
* [96. Unique Binary Search Trees](leetCode-96-Unique-Binary-Search-Trees.md)
100-
* [97. Interleaving String](leetCode-97-Interleaving-String.md)
100+
* [97. Interleaving String](leetCode-97-Interleaving-String.md)
101+
* [98. Validate Binary Search Tree](leetCode-98-Validate-Binary-Search-Tree.md)

leetCode-42-Trapping-Rain-Water.md

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,13 +108,23 @@ public int trap(int[] height) {
108108
}
109109
return sum;
110110
}
111+
112+
private int getMax(int[] height) {
113+
int max = 0;
114+
for (int i = 0; i < height.length; i++) {
115+
if (height[i] > max) {
116+
max = height[i];
117+
}
118+
}
119+
return max;
120+
}
111121
```
112122

113123
时间复杂度:如果最大的数是 m,个数是 n,那么就是 O(m * n)。
114124

115125
空间复杂度: O (1)。
116126

117-
下边讲一下, leetcode [solution](https://leetcode.com/problems/trapping-rain-water/solution/) 提供的 4 个算法。
127+
经过他人提醒,这个解法现在 AC 不了了,会报超时,但还是放在这里吧。 下边讲一下, leetcode [solution](https://leetcode.com/problems/trapping-rain-water/solution/) 提供的 4 个算法。
118128

119129
# 解法二 按列求
120130

leetCode-76-Minimum-Window-Substring.md

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -132,13 +132,13 @@ private boolean match(Map<Character, Integer> map) {
132132
}
133133
```
134134

135-
时间复杂度:O(n),n 是 S 的长度。
135+
时间复杂度:O(nm),n 是 S 的长度,match 函数消耗 O(m)
136136

137137
空间复杂度:O(m),m 是 T 的长度。
138138

139139
参考[这里](<https://leetcode.com/problems/minimum-window-substring/discuss/26835/Java-4ms-bit-97.6>),由于字符串中只有字母,我们其实可以不用 hashmap,可以直接用一个 int 数组,字母的 ascii 码值作为下标,保存每个字母的次数。
140140

141-
此外,判断当前窗口是否含有所有字母,我们除了可以判断所有字母的次数是否小于等于 0,还可以用一个计数变量 count,把 count 初始化为 t 的长度,然后每次找到一个满足条件的字母,count 就减 1,如果 count 等于了 0,就代表包含了所有字母。
141+
此外,判断当前窗口是否含有所有字母,我们除了可以判断所有字母的次数是否小于等于 0,还可以用一个计数变量 count,把 count 初始化为 t 的长度,然后每次找到一个满足条件的字母,count 就减 1,如果 count 等于了 0,就代表包含了所有字母。这样的话,可以把之前的 match(map) 优化到 O(1)。
142142

143143
```java
144144
public String minWindow(String s, String t) {
@@ -192,8 +192,6 @@ public String minWindow(String s, String t) {
192192
}
193193
```
194194

195-
用数组改写以后时间复杂度没有变,但速度快了很多。
196-
197195
#
198196

199197
开始自己的思路偏了,一直往递归,动态规划的思想走,导致没想出来。对滑动窗口的应用的少,这次加深了印象。

leetCode-94-Binary-Tree-Inorder-Traversal.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -59,18 +59,18 @@ getAns(node.right, ans); //压栈
5959
/ \ /
6060
4 5 6
6161

62-
push push push pop push pop pop pop
63-
| | | | |_4_| | | |_5_| | | | | | |
64-
| | |_2_| |_2_| |_2_| |_2_| |_2_| | | | |
65-
|_1_| |_1_| |_1_| |_1_| |_1_| |_1_| |_1_| | |
66-
ans add 4 add 5 add 2 add 1
67-
[] [4] [4 5] [4 5 2][4 5 2 1]
62+
push push push pop pop push pop pop
63+
| | | | |_4_| | | | | | | | | | |
64+
| | |_2_| |_2_| |_2_| | | |_5_| | | | |
65+
|_1_| |_1_| |_1_| |_1_| |_1_| |_1_| |_1_| | |
66+
ans add 4 add 2 add 5 add 1
67+
[] [4] [4 2] [4 2 5] [4 2 5 1]
6868
push push pop pop
6969
| | | | | | | |
7070
| | |_6_| | | | |
7171
|_3_| |_3_| |_3_| | |
7272
add 6 add 3
73-
[4 5 2 1 6] [4 5 2 1 6 3]
73+
[4 2 5 1 6] [4 2 5 1 6 3]
7474
```
7575

7676
结合代码。

0 commit comments

Comments
 (0)