当前位置:网站首页>【刷题篇】接雨水(一维)
【刷题篇】接雨水(一维)
2022-07-03 03:52:00 【m0_60631323】
一、题目
OJ链接
给定 n 个非负整数表示每个宽度为 1 的柱子的高度图,计算按此排列的柱子,下雨之后能接多少雨水。
二、题解
2.1思路
2.2源码
public int trap(int[] height) {
if(height==null||height.length<2){
return 0;
}
int N=height.length;
int leftMax=height[0];
int rightMax=height[N-1];
int L=1;
int R=N-2;
int water=0;
while(L<=R){
if(leftMax<=rightMax){
water+=Math.max(0,leftMax-height[L]);
leftMax=Math.max(leftMax,height[L++]);
}else {
water+=Math.max(0,rightMax-height[R]);
rightMax=Math.max(rightMax,height[R--]);
}
}
return water;
}
边栏推荐
- How to download pytorch? Where can I download pytorch?
- Makefile demo
- 在 .NET 6 项目中使用 Startup.cs
- navicat 导出数据库的表结构
- Makefile demo
- 阿洛对自己的思考
- 2022 tea master (intermediate) examination questions and analysis and tea master (intermediate) practical examination video
- PHP generates PDF tcpdf
- Ffmpeg recording screen and screenshot
- Web会话管理安全问题
猜你喜欢
学会pytorch能干什么?
pytorch是什么?pytorch是一个软件吗?
pytorch项目怎么跑?
没有sXid,suid&sgid将进入险境!-尚文网络xUP楠哥
释放数据力量的Ceph-尚文网络xUP楠哥
105. SAP UI5 Master-Detail 布局模式的联动效果实现明细介绍
Recursion: quick sort, merge sort and heap sort
第十届中国云计算大会·中国站:展望未来十年科技走向
递归:快速排序,归并排序和堆排序
Small guide for rapid formation of manipulator (VIII): kinematic modeling (standard DH method)
随机推荐
Mysql Mac版下载安装教程
2022 mobile crane driver examination registration and mobile crane driver operation examination question bank
2022-07-02:以下go语言代码输出什么?A:编译错误;B:Panic;C:NaN。 package main import “fmt“ func main() { var a =
2022 P cylinder filling examination content and P cylinder filling practice examination video
Basic operations of mongodb [add, delete, modify, query]
Docker install and start MySQL service
Nanning water leakage detection: warmly congratulate Guangxi Zhongshui on winning the first famous brand in Guangxi
[Blue Bridge Road -- bug free code] interpretation of some codes of matrix keyboard
[DRM] simple analysis of DRM bridge driver call process
C语言HashTable/HashSet库汇总
[learning notes] seckill - seckill project - (11) project summary
TCP, the heavyweight guest in tcp/ip model -- Kuige of Shangwen network
How to execute a swift for in loop in one step- How can I do a Swift for-in loop with a step?
用Three.js做一個簡單的3D場景
Ansible introduction [unfinished (semi-finished products)]
FileZilla client download and installation
MongoDB简介
记一次 .NET 差旅管理后台 CPU 爆高分析
Download and install node, NPM and yarn
Use of sigaction