42. Trapping Rain Water
2016-06-05 19:31
246 查看
题目:https://leetcode.com/problems/trapping-rain-water/
代码:
代码:
public class Solution { public int trap(int[] height) { int i=0,j,m; int res = 0; while(i<height.length) { j = higher(height,i); if(j==-1) { m = i; i = nexthigh(height,i); if(i==-1) break; res += height[i]*(i-m-1); for(int n=m+1;n<i;n++) res -= height ; continue; } res += (j-i-1)*Math.min(height[j],height[i]); for(int temp=i+1;temp<j;temp++) res -= height[temp]; i = j; } return res; } public int higher(int[] height,int i) { int flag = height[i]; for(int j=i+1;j<height.length;j++) { if(height[j]>=flag) return j; } return -1; } public int nexthigh(int[] height,int i) { int res = i+1; if(res==height.length) return -1; for(int j=i+1;j<height.length;j++) { if(height[j]>height[res]) res = j; } 4000 return res; } } 2ms 基本思路:高柱子和高柱子之间夹击,之后在剩下的数组值中找最高夹击
相关文章推荐
- Android的Handler,Looper源码剖析
- 文章标题
- android.text.format.DateFormat 日期时间解析类
- 202. Happy Number
- iOS中几种常见的查找算法的比较
- Android--color.xml的值
- 【阅读笔记】移动APP测试实战--第一章
- CocoaPods 安装和使用
- iOS算法篇(二)选择排序算法
- iOS算法篇(一)快速排序算法
- 浅谈android中异步加载之"取消异步加载"二
- AsyncTask源码分析之Android篇
- iOS 简单的runtime替换方法
- Android 从服务器获取时间戳和当前时间算出时间差
- iOS动画概述大全
- EventBus笔记(一)
- swift学习之路(十一)函数
- android产品研发(七)-->Apk热修复
- Unity3d优化
- iOS开发中几种常用设计模式