博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[LeetCode] Trapping Rain Water
阅读量:6802 次
发布时间:2019-06-26

本文共 2746 字,大约阅读时间需要 9 分钟。

Note: The following idea is in fact from the last answer in , which leads to a clean code. I just reorganize it and add some explanations. I hope it is Ok.

The following are four solutions in C/C++/Java/Python respectively. The basic idea is that we set two pointers l and r to the left and right end of height. Then we get the minimum height (minHeight) of these pointers (similar to Container with Most Water due to the Leaking Bucket Effect) since the level of the water cannot be higher than it. Then we move the two pointers towards the center. If the coming level is less than minHeight, then it will hold some water. Fill the water until we meet some "barrier" (with height larger than minHeight) and update l and r to repeat this process in a new interval.


C

1 int trap(int* height, int heightSize) { 2     int l = 0, r = heightSize - 1, water = 0, minHeight = 0; 3     while (l < r) {  4         while (l < r && height[l] <= minHeight) 5             water += minHeight - height[l++]; 6         while (r > l && height[r] <= minHeight) 7             water += minHeight - height[r--]; 8         minHeight = height[l] <= height[r] ? height[l] : height[r]; 9     }10     return water;11 }

C++

1 class Solution { 2 public: 3     int trap(vector
& height) { 4 int n = height.size(), l = 0, r = n - 1, water = 0, minHeight = 0; 5 while (l < r) { 6 while (l < r && height[l] <= minHeight) 7 water += minHeight - height[l++]; 8 while (r > l && height[r] <= minHeight) 9 water += minHeight - height[r--];10 minHeight = min(height[l], height[r]);11 }12 return water;13 }14 };

Java

public class Solution {    public int trap(int[] height) {        int n = height.length, l = 0, r = n - 1, water = 0, minHeight = 0;        while (l < r) {            while (l < r && height[l] <= minHeight)                water += minHeight - height[l++];            while (r > l && height[r] <= minHeight)                water += minHeight - height[r--];            minHeight = Math.min(height[l], height[r]);        }        return water;    }}

Python

1 class Solution: 2     # @param {integer[]} height 3     # @return {integer}  4     def trap(self, height): 5         n = len(height) 6         l, r, water, minHeight = 0, n - 1, 0, 0 7         while l < r: 8             while l < r and height[l] <= minHeight: 9                 water += minHeight - height[l]10                 l += 111             while r > l and height[r] <= minHeight:12                 water += minHeight - height[r]13                 r -= 114             minHeight = min(height[l], height[r])15         return water

 

转载于:https://www.cnblogs.com/jcliBlogger/p/4646980.html

你可能感兴趣的文章
TypeError: datetime is not JSON serializable
查看>>
我的友情链接
查看>>
下一代大数据处理引擎,阿里云实时计算独享模式重磅发布
查看>>
RAM SSO功能重磅发布 —— 满足客户使用企业本地账号登录阿里云
查看>>
Pyscripter为什么总报错?UnicodeEncodeError: 'ascii' codec
查看>>
linux内核之系统启动(二)
查看>>
IBM MQ 7.5开发版安装配置
查看>>
走出IT管理员与IT工程师的误区
查看>>
How-To Install ELK Stack(Elasticsearch, Logstash, and Kibana ) Success Version
查看>>
CentOS Linux服务器安全设置
查看>>
MySQL 5.5.x 单机多实例配置实践
查看>>
网络规划设计师-2011年下半年成绩
查看>>
PHP学习笔记【11】--PHP数组
查看>>
Hibernate N+1/1+N问题
查看>>
Nginx的反向代理及负载均衡
查看>>
Shell 十三问学习笔记5
查看>>
华为PPP链路认证
查看>>
Zend Server 安装配置
查看>>
wuzhicms后台菜单的添加
查看>>
hadoop搭建
查看>>