8000 提交接雨水java解法 · coder-chenhao/fucking-algorithm@5967110 · GitHub
[go: up one dir, main page]

Skip to content

Commit 5967110

Browse files
kptnewlerlabuladong
authored andcommitted
提交接雨水java解法
1 parent 5f02952 commit 5967110

File tree

1 file changed

+83
-1
lines changed

1 file changed

+83
-1
lines changed

高频面试系列/接雨水.md

Lines changed: 83 additions & 1 deletion
< 10000 td data-grid-cell-id="diff-e8c6636e6339d9e4722d896b0619bfc6c1203bc6ed5fd0161be9552d2f6bb7e0-185-215-1" data-selected="false" role="gridcell" style="background-color:var(--diffBlob-additionNum-bgColor, var(--diffBlob-addition-bgColor-num));text-align:center" tabindex="-1" valign="top" class="focusable-grid-cell diff-line-number position-relative left-side">215
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,88 @@ if (l_max < r_max) {
183183

184184
![labuladong](../pictures/labuladong.jpg)
185185

186+
暴力解法
187+
188+
```java
189+
public int trap(int[] height) {
190+
int ans = 0;
191+
for (int i = 1; i < height.length - 1; i++) {
192+
int leftMax = 0, rightMax = 0;
193+
// 找右边最高的柱子
194+
for (int j = i; j < height.length; j++) {
195+
rightMax = Math.max(height[j], rightMax);
196+
}
197+
// 找左边最高的柱子
198+
for (int j = i; j >= 0; j--) {
199+
leftMax = Math.max(height[j], leftMax);
200+
}
201+
// 如果自己就是最高的话,
202+
// leftMax == rightMax == height[i]
203+
ans += Math.min(leftMax, rightMax) - height[i];
204+
}
205+
return ans;
206+
}
207+
```
208+
209+
备忘录优化解法
210+
211+
```java
212+
public int trap(int[] height) {
213+
if (height == null || height.length == 0) return 0;
214+
int ans = 0;
+
// 数组充当备忘录
216+
int[] leftMax = new int[height.length];
217+
int[] rightMax = new int[height.length];
218+
// 初始化base case
219+
leftMax[0] = height[0];
220+
rightMax[height.length - 1] = height[height.length - 1];
221+
// 从左到右计算leftMax
222+
for (int i = 1; i < height.length; i++) {
223+
leftMax[i] = Math.max(height[i], leftMax[i-1]);
224+
}
225+
// 从右到左计算rightMax
226+
for (int i = height.length - 2; i >= 0; i--) {
227+
rightMax[i] = Math.max(height[i], rightMax[i + 1]);
228+
}
229+
// 计算结果
230+
for (int i = 1; i < height.length - 1; i++) {
231+
ans += Math.min(leftMax[i], rightMax[i]) - height[i];
232+
}
233+
return ans;
234+
}
235+
```
236+
237+
双指针解法
238+
239+
```java
240+
public int trap(int[] height) {
241+
if (height == null || height.length == 0) return 0;
242+
243+
int ans = 0;
244+
int leftMax, rightMax;
245+
// 左右指针
246+
int left = 0, right = height.length - 1;
247+
// 初始化
248+
leftMax = height[0];
249+
rightMax = height[height.length - 1];
250+
251+
while (left < right) {
252+
// 更新左右两边柱子最大值
253+
leftMax = Math.max(height[left], leftMax);
254+
rightMax = Math.max(height[right], rightMax);
255+
256+
// 相当于ans += Math.min(leftMax, rightMax) - height[i]
257+
if (leftMax < rightMax) {
258+
ans += leftMax - height[left];
259+
left++;
260+
} else {
261+
ans += rightMax - height[right];
262+
right--;
263+
}
264+
}
265+
return ans;
266+
}
267+
```
186268

187269
[eric wang](https://www.github.com/eric496) 提供 Java 代码
188270

@@ -248,4 +330,4 @@ def trap(self, height: List[int]) -> int:
248330

249331
[下一篇:如何去除有序数组的重复元素](../高频面试系列/如何去除有序数组的重复元素.md)
250332

251-
[目录](../README.md#目录)
333+
[目录](../README.md#目录)

0 commit comments

Comments
 (0)
0