Leetcode 42 Trapping Rain Water
来源:互联网 发布:js作用域链 编辑:程序博客网 时间:2024/05/17 01:48
Given n non-negative integers representing an elevation map where the width of each bar is 1, compute how much water it is able to trap after raining.
For example,
Given [0,1,0,2,1,0,1,3,2,1,2,1]
, return 6
.
双指针的问题啊,重点在于,怎么判断当前能够存住水。
思考一下,存住的水取决于左右两块板的高度,首先要都大于零,并且受短板的牵制。
plank就相当于从两侧开始造一个木桶,plank的大小就是高度,只增不减,遇到比plank矮的就能储水
如果继续遇到新的plank就更新plank的值
public class Solution { public int trap(int[] A) { int i = 0, j = A.length - 1, result = 0, plank = 0; while(i <= j){ plank = plank < Math.min(A[i], A[j]) ? Math.min(A[i], A[j]) : plank; result = A[i] >= A[j] ? result + (plank - A[j--]) : result + (plank - A[i++]); } return result; }}
阅读全文
0 0
- [leetcode 42] Trapping Rain Water
- leetcode || 42、 Trapping Rain Water
- [LeetCode 42]Trapping Rain Water
- LeetCode(42) Trapping Rain Water
- Leetcode 42: Trapping Rain water
- LeetCode---(42)Trapping Rain Water
- leetcode 42: Trapping Rain Water
- LeetCode-42 Trapping Rain Water
- Leetcode#42||Trapping Rain Water
- Leetcode #42 Trapping Rain Water
- leetcode 42 Trapping Rain Water
- leetcode-42 Trapping Rain Water
- leetcode 42:Trapping Rain Water
- LeetCode 42 Trapping Rain Water
- LeetCode 42 - Trapping Rain Water
- [leetcode] 42 Trapping Rain Water
- LeetCode(42)-Trapping Rain Water
- LeetCode 42 Trapping Rain Water
- 写作,解题,斗地主,红龙,MOBA
- spring自定义注解
- multidex解决方法数越界
- 面向对象及组件开发(五)普通方法与原型方法
- Android NDK开发之旅(1): Eclipse中NDK环境搭建与JNI开发流程
- Leetcode 42 Trapping Rain Water
- TinyOS安装完成后使用TIOSSIM仿真的注意事项
- Docker实践sshd镜像
- matlab函数使用说明之——load函数
- 在 Linux 命令行脚本中执行 sudo 等命令时自动输入密码(使用expect)
- 两台服务器之间免密码传输文件scp ssh
- C#使用LitJson对Json数据解析
- NPM使用介绍
- javascript高级程序设计(4、5章)