时间: 2020-11-23|13次围观|0 条评论

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.

 

 

 1 class Solution {
 2 public:
 3     int trap(vector<int>& height) {
 4         int max=0;
 5         for(int i=0;i<height.size();i++)
 6         {
 7             if(height[i]>height[max])
 8                 max=i;
 9         }
10 
11         int water=0;
12 
13         for(int i=0,peak=0;i<max;i++)
14             if(height[i]>peak)
15                 peak=height[i];
16             else
17                 water=water+peak-height[i];
18 
19         for(int i=height.size()-1,top=0;i>max;i--)
20             if(height[i]>top)
21                 top=height[i];
22             else
23                 water=water+top-height[i];
24 
25         return water;
26     }
27 };

 

转载于:https://www.cnblogs.com/jawiezhu/p/4486950.html

原文链接:https://blog.csdn.net/weixin_30342827/article/details/98758166

本站声明:网站内容来源于网络,如有侵权,请联系我们,我们将及时处理。

本博客所有文章如无特别注明均为原创。
复制或转载请以超链接形式注明转自起风了,原文地址《【leetcode】Trapping Rain Water
   

还没有人抢沙发呢~