042 - Trapping Rain Water
来源:互联网 发布:同花顺官方炒股软件 编辑:程序博客网 时间:2024/06/09 15:30
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
.
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!
int findleft(int *height, int size, int loc){if (height[loc - 1] <= height[loc]) return -1;else return loc - 1;}int findright(int *height, int size, int loc){int i, right = -1;for (i = loc + 1; i < size; i++) {if ((height[i] > height[loc]))if (right < 0)right = i;else if (height[i] > height[right])right = i;if (height[right] >= height[loc - 1]) break;}return right;}int trap(int* height, int heightSize) {int i, j, left, right;int ret = 0;for (i = 1; i < heightSize - 1; i++) {left = findleft(height, heightSize, i);right = findright(height, heightSize, i);if (left >= 0 && right > 0) {int h = height[left] < height[right]? height[left]:height[right];for (j = left + 1; j < right; j++) {ret += h - height[j];}i = right;}}return ret;}
0 0
- 042 - Trapping Rain Water
- leetcode-042:Trapping Rain Water
- LeetCode 042 Trapping Rain Water
- [LeetCode]042-Trapping Rain Water
- LeetCode 042 Trapping Rain Water
- leetcode 042 —— Trapping Rain Water
- leetcode之路042 Trapping Rain Water
- leetcode解题方案--042--Trapping Rain Water
- LeetCode: Trapping Rain Water
- LeetCode : Trapping Rain Water
- Trapping Rain Water
- [LeetCode] Trapping Rain Water
- [Leetcode] Trapping Rain Water
- [Leetcode] Trapping Rain Water
- Trapping Rain Water
- Trapping Rain Water
- [LeetCode]Trapping Rain Water
- Trapping Rain Water
- Android一点 适配
- Java多线程用法解析
- C#:JSON嵌套对象反系列化,读取对象的值
- Hadoop平台优化综述
- sql server 2012还原到sql server 2008
- 042 - Trapping Rain Water
- 多线程概述
- C语言-----循环双向链表(增加文件读写链表功能)
- POJ 3278 Catch That Cow
- HDU 1712 分组背包
- Java synchronized
- C++primer plus第六版课后编程练习答案4.7
- Spring+ibatis 应用实例
- VBox的Linux虚拟机,共享文件夹