题目:

代码(首刷自解 2024年3月2日 有3个案例超时):
不算完全做出来,看了答案了,等以后二刷吧
class Solution {
public:
    int helper(const vector<int>& height,const int high) {
        for(int i = 0; i < height.size(); ++i) {
            if (height[i] > high) {
                return i;
            }
        }
        return 0;
    }
    int trap(vector<int>& height) {
        int result = 0;
        int high = 0;
        int firstIndex = helper(height, 0);
        while (true) {
            high++;
            int left = firstIndex;
            int right = firstIndex;
            for (right = firstIndex; right < height.size(); ++right) {
                if (height[right] < high) continue;
                if (right - left > 1) {
                    result += right - left - 1;
                }
                left = right;
            }
            if (left == firstIndex) break;
            firstIndex = helper(height, high);
        }
        return result;
    }
};


















