From c81726022d9b0e9c71d192411880fbcb8c09ac26 Mon Sep 17 00:00:00 2001 From: yuyu <2750167653@qq.com> Date: Fri, 16 May 2025 15:07:04 +0800 Subject: [PATCH] =?UTF-8?q?Update=20Leetcode=20=E9=A2=98=E8=A7=A3=20-=20?= =?UTF-8?q?=E5=8F=8C=E6=8C=87=E9=92=88.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 更新接雨水题目,该题目在面试中经常出现 --- ...- \345\217\214\346\214\207\351\222\210.md" | 64 +++++++++++++++++++ 1 file changed, 64 insertions(+) diff --git "a/notes/Leetcode \351\242\230\350\247\243 - \345\217\214\346\214\207\351\222\210.md" "b/notes/Leetcode \351\242\230\350\247\243 - \345\217\214\346\214\207\351\222\210.md" index 1dbd48df9c..ff7dd075c6 100644 --- "a/notes/Leetcode \351\242\230\350\247\243 - \345\217\214\346\214\207\351\222\210.md" +++ "b/notes/Leetcode \351\242\230\350\247\243 - \345\217\214\346\214\207\351\222\210.md" @@ -8,6 +8,7 @@ * [5. 归并两个有序数组](#5-归并两个有序数组) * [6. 判断链表是否存在环](#6-判断链表是否存在环) * [7. 最长子序列](#7-最长子序列) + * [8. 接雨水](#8-动态规划+双指针) @@ -292,3 +293,66 @@ private boolean isSubstr(String s, String target) { return j == target.length(); } ``` + + +## 8. 接雨水 +42\.Collect rainwater(difficult) + +[Leetcode](https://leetcode.cn/problems/trapping-rain-water/description/?envType=study-plan-v2&envId=top-100-liked) / [力扣](https://leetcode.cn/problems/trapping-rain-water/description/?envType=study-plan-v2&envId=top-100-liked) + + +``` +Input: +height = [0,1,0,2,1,0,1,3,2,1,2,1] + +Output: +6 +``` +题目描述:给定 n 个非负整数表示每个宽度为 1 的柱子的高度图,计算按此排列的柱子,下雨之后能接多少雨水。 + + +```java + +class Solution { + public int trap(int[] height) { + int n = height.length; + if (n == 0) return 0; + + int[] l = new int[n]; + int[] r = new int[n]; + + l[0] = height[0]; + r[n - 1] = height[n - 1]; + + // 从左到右构建最大高度数组 + for (int i = 1; i < n; i++) { + l[i] = Math.max(height[i], l[i - 1]); + } + + // 从右到左构建最大高度数组 + for (int i = n - 2; i >= 0; i--) { + r[i] = Math.max(height[i], r[i + 1]); + } + + int res = 0; + for (int i = 0; i < n; i++) { + res += Math.min(l[i], r[i]) - height[i]; + } + + return res; + } +} + + +``` + + + + + + + + + + +