leetcode.42. Trapping Rain Water
2016-05-09 22:28
141 查看
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
The above elevation map is represented by array [0,1,0,2,1,0,1,3,2,1,2,1]. In this case, 6 units of rain water (blue section) are being trapped.
Thanks Marcos for contributing this image!
For example,
Given
[0,1,0,2,1,0,1,3,2,1,2,1], return
6.
The above elevation map is represented by array [0,1,0,2,1,0,1,3,2,1,2,1]. In this case, 6 units of rain water (blue section) are being trapped.
Thanks Marcos for contributing this image!
class Solution { public: int trap(vector<int>& height) { int res = 0; int l = 0; int r = height.size() - 1; while(l < r) { int minh = min(height[l], height[r]); if(height[l] == minh) while(++ l < r && height[l] < minh) res += minh - height[l]; else while(l < -- r && height[r] < minh) res += minh - height[r]; } return res; } };
相关文章推荐
- Android SparseArray的用法
- Android 中文API:Signing Your Applications(为APK签名)
- 在android中用eventBus.2.4.jar中出现的问题
- iOS - (nullable NSString *)stringByAddingPercentEscapesUsingEncoding:(NSStringEncoding)enc 代替方法的使用
- iOS SDK开发时,有关OC和C++混编造成其它类错误的问题(Compile Sources As Objective-C++)
- VS2015 create a C++ console application based on WinRT
- WebView使用小结
- Android开发——ListView的运用
- Android studio 图片显示过大 RadioButton
- android源码网
- 【翻译】 Unity3D VR 教程:4.VR中的用户界面
- E/AndroidRuntime: FATAL EXCEPTION: main
- Appium学习-IOS初窥
- Android 开发绕不过的坑:你的 Bitmap 究竟占多大内存?
- iOS 开发 带区号的城市三级联动(xml解析)
- Swift实现侧滑菜单SWRevealViewController
- Android自定义控件实现一个带文本与数字的圆形进度条
- Android Studio: Missing 32 bit compatbility library
- 微信支付
- APP开发实战6-APP的切图工作