Leetcode: Trapping Rain Water

來源:互聯網
上載者:User

標籤:style   blog   class   code   java   c   

這道題做的比較艱辛,一開始自己想的是一個用stack的解法,感覺過於繁瑣(出棧,入棧,計算容積),但未嘗不是一個好的嘗試,這個方法還是有點小問題,過後會好好想清楚。看了網上提示完成了最終的方法,這個方法兩次遍曆數組,第一次遍曆找每個元素右邊最大的元素,第二次遍曆尋找每個元素左邊最大的元素,同時計算該index可以存放的水容積: min{lefthighest, righthighest}-A[i]

最終方法:

 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 water31                 sum += min - A[j];32             }33         }34         return sum;35     }36 }

之前用stack嘗試解的方法,較複雜,但是是個不錯的思考切入點:

 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 elements12                 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 }

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.