1class Solution {2public:3int trap(vector<int> &height) {4int left = 0, right = height.size() - 1;5int maxleft = 0, maxright = 0;6int res = 0;7while (left <= right) {8if (height[left] <= height[right]) {9if (height[left] >= maxleft)10maxleft = height[left];11else12res += maxleft - height[left];13left++;14} else {15if (height[right] >= maxright)16maxright = height[right];17else18res += maxright - height[right];19right--;20}21}22return res;23}24};