【LeetCode】42. 接雨水(双指针/单调栈)

# [题目链接](https://leetcode-cn.com/problems/trapping-rain-water/) # 代码: ``` class Solution { public: int trap(vector & height) { int left = 0, right = height.size() - 1; int res = 0; int maxleft = 0, maxright = 0; while(left <= right){ if(height[left] <= height[right]){ if(height[left] > maxleft) maxleft = height[left]; else res += maxleft - height[left++]; }else{ if(height[right] > maxright) maxright = height[right]; else res += maxright - height[right--]; } } return res; } }; ```

猜你喜欢

转载自www.cnblogs.com/whisperbb/p/12634757.html
今日推荐