码迷,mamicode.com
首页 > 移动开发 > 详细

Leetcode: Trapping Rain Water

时间:2014-05-14 11:03:34      阅读:412      评论:0      收藏:0      [点我收藏+]

标签:style   blog   class   code   java   c   

这道题做的比较艰辛,一开始自己想的是一个用stack的解法,感觉过于繁琐(出栈,入栈,计算容积),但未尝不是一个好的尝试,这个方法还是有点小问题,过后会好好想清楚。看了网上提示完成了最终的方法,这个方法两次遍历数组,第一次遍历找每个元素右边最大的元素,第二次遍历寻找每个元素左边最大的元素,同时计算该index可以存放的水容积: min{lefthighest, righthighest}-A[i]

最终方法:

bubuko.com,布布扣
 1 public class Solution {
 2     public int trap(int[] A) {
 3         int length = A.length, sum = 0, min = 0;
 4         int[] lefthighest = new int[length];
 5         int[] righthighest = new int[length];
 6         
 7         for (int i=length-1; i>=0; i--) {
 8             if (i == length-1) {
 9                 righthighest[i] = 0;
10             }
11             else if (A[i+1] > righthighest[i+1]) {
12                 righthighest[i] = A[i+1];
13             }
14             else if (A[i+1] <= righthighest[i+1]) {
15                 righthighest[i] = righthighest[i+1];
16             }
17         }
18         
19         for (int j=0; j<length; j++) {
20             if (j==0) {
21                 lefthighest[j] = 0;
22             }
23             else if (A[j-1] > lefthighest[j-1]) {
24                 lefthighest[j] = A[j-1];
25             }
26             else if (A[j-1] <= lefthighest[j-1]) {
27                 lefthighest[j] = lefthighest[j-1];
28             }
29             min = Math.min(lefthighest[j], righthighest[j]);
30             if (min > A[j]) { //can store water
31                 sum += min - A[j];
32             }
33         }
34         return sum;
35     }
36 }
bubuko.com,布布扣

之前用stack尝试解的方法,较复杂,但是是个不错的思考切入点:

bubuko.com,布布扣
 1 public class Solution {
 2     public int trap(int[] A) {
 3         Stack<Integer> s = new Stack<Integer>();
 4         int i = 0, sum = 0, lastpop = 0;
 5         while (i < A.length){
 6             if (s.empty()) {
 7                 s.push(i);
 8                 i++;
 9                 continue;
10             }
11             if (A[s.peek()] < A[i]){ //can hold water if stack contains other elements
12                 while (A[s.peek()] < A[i]){
13                     lastpop = A[s.pop()];
14                     if (s.empty()){
15                         s.push(i);
16                         i++;
17                         continue;
18                     }
19                     sum += (i - s.peek() -1) * (A[s.peek()] - lastpop);
20                 }
21                 if (A[s.peek()] > A[i]){
22                     sum += (i - s.peek() -1) * (A[i] - lastpop);
23                     s.push(i);
24                     i++;
25                     continue;
26                 }
27                 if (A[s.peek()] == A[i]){
28                     sum += (i - s.peek() -1) * (A[i] - lastpop);
29                     s.pop();
30                     i++;
31                     continue;
32                 }
33             }
34             else if (s.peek() == A[i]){
35                 i++;
36                 continue;
37             }
38             else if (s.peek() > A[i]){
39                 s.push(i);
40                 i++;
41                 continue;
42             }
43         }
44         return sum;
45     }
46 }
bubuko.com,布布扣

 

Leetcode: Trapping Rain Water,布布扣,bubuko.com

Leetcode: Trapping Rain Water

标签:style   blog   class   code   java   c   

原文地址:http://www.cnblogs.com/EdwardLiu/p/3726903.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!