Java for LeetCode 042 Trapping Rain Water
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.
解题思路:
先找到第一块最高的木板,然后从前向最高木板遍历,接着从后向最高木板遍历,JAVA实现如下:
static public int trap(int[] height) {
int result=0,peak=0,firstMax=0;
for(int i=0;i<height.length;i++)
if(height[i]>height[firstMax])
firstMax=i;
for(int i=0;i<firstMax;i++)
if(height[i]>height[peak])
peak=i;
else result+=height[peak]-height[i];
peak=height.length-1;
for(int i=height.length-1;i>firstMax;i--)
if(height[i]>height[peak])
peak=i;
else result+=height[peak]-height[i];
return result;
}
Java for LeetCode 042 Trapping Rain Water的更多相关文章
- LeetCode 042 Trapping Rain Water
题目要求:Trapping Rain Water Given n non-negative integers representing an elevation map where the width ...
- [leetcode][042] Trapping Rain Water (Java)
我在Github上新建了一个解答Leetcode问题的Project, 大家可以参考, 目前是Java 为主,里面有leetcode上的题目,解答,还有一些基本的单元测试,方便大家起步. 题目在这里: ...
- [LeetCode] 407. Trapping Rain Water II 收集雨水 II
Given an m x n matrix of positive integers representing the height of each unit cell in a 2D elevati ...
- leetcode#42 Trapping rain water的五种解法详解
leetcode#42 Trapping rain water 这道题十分有意思,可以用很多方法做出来,每种方法的思想都值得让人细细体会. 42. Trapping Rain WaterGiven n ...
- [array] leetcode - 42. Trapping Rain Water - Hard
leetcode - 42. Trapping Rain Water - Hard descrition Given n non-negative integers representing an e ...
- LeetCode 42. Trapping Rain Water 【两种解法】(python排序遍历,C++ STL map存索引,时间复杂度O(nlogn))
LeetCode 42. Trapping Rain Water Python解法 解题思路: 本思路需找到最高点左右遍历,时间复杂度O(nlogn),以下为向左遍历的过程. 将每一个点的高度和索引存 ...
- [LeetCode] 42. Trapping Rain Water 收集雨水
Given n non-negative integers representing an elevation map where the width of each bar is 1, comput ...
- [LeetCode] 407. Trapping Rain Water II 收集雨水之二
Given an m x n matrix of positive integers representing the height of each unit cell in a 2D elevati ...
- LeetCode - 42. Trapping Rain Water
42. Trapping Rain Water Problem's Link ------------------------------------------------------------- ...
随机推荐
- 列表 list
例子: #!/usr/bin/python li = list([11,22,33,66,99,77]) print(li) li.extend((44,55,)) print(li) ret = l ...
- adb devices找不着设备
第1步,查找设备VID VID是04E8,那么打开C:\Users\liujuan\Documents\.android\adb_usb.ini 在里面添加上设备即是 0x04E8,保存并关闭 然后c ...
- HTTPS-能否避免流量劫持
流量劫持是什么? EtherDream在一篇科普文章<>中详细介绍了流量劫持途径和方式. 流量劫持是一种古老的攻击方式,比如早已见惯的广告弹窗等,很多人已经对此麻木,并认为流量劫持不会造成 ...
- safeNet
把那4个dll丢到C:\Windows\SysWOW64里去重启IIS,再测试
- xss跨站实例总结
跨站脚本攻击(Cross-site scripting,通常简称为XSS)发生在客户端,可被用于进行窃取隐私.钓鱼欺骗.偷取密码.传播恶意代码等攻击行为. 恶意的攻击者将对客户端有危害的代码放到服务器 ...
- java.lang.NoClassDefFoundError: org/hibernate/cfg/Configuration解决方法
Autowiring of fields failed; nested exception is...........Error creating bean with name 'siteOperat ...
- Linux中用st_mode判断文件类型
Linux中用st_mode判断文件类型 2012-12-11 12:41 14214人阅读 评论(4) 收藏 举报 分类: Linux(8) C/C++(20) 版权声明:本文为博主原创文章, ...
- linux下ping的C语言实现(转)
#include <stdio.h> #include <signal.h> #include <arpa/inet.h> #include <sys/typ ...
- absolute布局和css布局释疑
jqueryui也不是万能的, 有时候, 也需要自己写一些, 由 css 和jquery结合的一些东西, 如: banner中, 依次播放的div等 ## 关于jquery设计的一些思想和理念?but ...
- 微信JS-SDK
<div class="lbox_close wxapi_form"> <h3 id="menu-basic">基础接口</h3& ...