算法leetcode|42. 接雨水(rust重拳出击)
创始人
2025-05-31 02:29:07

文章目录

  • 42. 接雨水:
    • 样例 1:
    • 样例 2:
    • 提示:
  • 分析:
  • 题解:
    • rust
    • go
    • c++
    • c
    • python
    • java


42. 接雨水:

给定 n 个非负整数表示每个宽度为 1 的柱子的高度图,计算按此排列的柱子,下雨之后能接多少雨水。

样例 1:

输入:height = [0,1,0,2,1,0,1,3,2,1,2,1]输出:6解释:上面是由数组 [0,1,0,2,1,0,1,3,2,1,2,1] 表示的高度图,在这种情况下,可以接 6 个单位的雨水(蓝色部分表示雨水)。 

样例 2:

输入:height = [4,2,0,3,2,5]输出:9

提示:

  • n == height.length
  • 1 <= n <= 2 * 104
  • 0 <= height[i] <= 105

分析:

  • 面对这道算法题目,二当家的陷入了沉思。
  • 单看一个位置能接多少雨水,其实是看它左面的最高高度,和右面的最高高度,两者取小,再减去当前位置的高度。
  • 如果每一个位置都去计算左右两侧的最高高度,时间复杂度就会是O(n2)
    ,太慢了,不能忍。
  • 在过程中存储左边最高高度和右面最高高度,复用才是正道。
  • 遍历的顺序怎么选?无论从左到右还是从右到左,另一边的最高高度都不便计算。
  • 那就两边一起遍历吧,那个熟悉的感觉,双指针。

题解:

rust

impl Solution {pub fn trap(height: Vec) -> i32 {let mut ans = 0;let (mut left, mut right) = (0, height.len() - 1);let (mut left_max, mut right_max) = (0, 0);while left < right {left_max = left_max.max(height[left]);right_max = right_max.max(height[right]);if height[left] < height[right] {ans += left_max - height[left];left += 1;} else {ans += right_max - height[right];right -= 1;}}return ans;}
}

go

func trap(height []int) (ans int) {max := func(a, b int) int {if a > b {return a}return b}left, right := 0, len(height)-1leftMax, rightMax := 0, 0for left < right {leftMax = max(leftMax, height[left])rightMax = max(rightMax, height[right])if height[left] < height[right] {ans += leftMax - height[left]left++} else {ans += rightMax - height[right]right--}}return
}

c++

class Solution {
public:int trap(vector& height) {int ans = 0;int left = 0, right = height.size() - 1;int leftMax = 0, rightMax = 0;while (left < right) {leftMax = max(leftMax, height[left]);rightMax = max(rightMax, height[right]);if (height[left] < height[right]) {ans += leftMax - height[left];++left;} else {ans += rightMax - height[right];--right;}}return ans;}
};

c

int trap(int* height, int heightSize){int ans = 0;int left = 0, right = heightSize - 1;int leftMax = 0, rightMax = 0;while (left < right) {leftMax = fmax(leftMax, height[left]);rightMax = fmax(rightMax, height[right]);if (height[left] < height[right]) {ans += leftMax - height[left];++left;} else {ans += rightMax - height[right];--right;}}return ans;
}

python

class Solution:def trap(self, height: List[int]) -> int:ans = 0left, right = 0, len(height) - 1left_max = right_max = 0while left < right:left_max = max(left_max, height[left])right_max = max(right_max, height[right])if height[left] < height[right]:ans += left_max - height[left]left += 1else:ans += right_max - height[right]right -= 1return ans

java

class Solution {public int trap(int[] height) {int ans = 0;int left = 0, right = height.length - 1;int leftMax = 0, rightMax = 0;while (left < right) {leftMax = Math.max(leftMax, height[left]);rightMax = Math.max(rightMax, height[right]);if (height[left] < height[right]) {ans += leftMax - height[left];++left;} else {ans += rightMax - height[right];--right;}}return ans;}
}

非常感谢你阅读本文~
欢迎【点赞】【收藏】【评论】~
放弃不难,但坚持一定很酷~
希望我们大家都能每天进步一点点~
本文由 二当家的白帽子:https://le-yi.blog.csdn.net/ 博客原创~


相关内容

热门资讯

玛雅人的五大预言 玛雅人预言2... 曾经玛雅人预言2012年是世界末日,但当时好像没有发生什么。没想到10年后的2022年,疫情,战争,...
cad打印线条粗细设置 cad... 004-线型(下)打印样式设置和线型文件使用一、线宽设置方法制图规范里边的线宽要求,我们已经定义好,...
应用未安装解决办法 平板应用未... ---IT小技术,每天Get一个小技能!一、前言描述苹果IPad2居然不能安装怎么办?与此IPad不...
荼蘼什么意思 岁月缱绻葳蕤生香... 感谢作者【辰夕】的原创独家授权分享编辑整理:【多肉植物百科】百科君坐标:云南 曲靖春而至,季节流转,...
长白山自助游攻略 吉林长白山游... 昨天介绍了西坡的景点详细请看链接:一个人的旅行,据说能看到长白山天池全凭运气,您的运气如何?今日介绍...