首页 > 学院 > 开发设计 > 正文

LeetCode 42 --- Trapping Rain Water

2019-11-08 02:18:15
字体:
来源:转载
供稿:网友

题目链接: LeetCode 42 — Trapping Rain Water

问题分析: 贪心思路,很简单。

AC代码:

public class PRoblem42 { public static void main(String[] args) { // TODO Auto-generated method stub/ int[] a={}; System.out.println(trap(a)); } public static int trap(int[] height) { int i=0,j=height.length-1; if(i>j) return 0; int maxleft = height[i]; int maxright= height[j]; int value =0; while(i<j){ if(height[i]<=height[j]){ i++; if(height[i]<maxleft){ value+=maxleft-height[i]; continue; } else{ maxleft=height[i]; continue; } } else{ j--; if(height[j]<maxright){ value+=maxright-height[j]; continue; } else{ maxright=height[j]; continue; } } } return value; }}
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表