LeetCode42-接雨水

int trap(vector<int>& height) {
    int res = 0;
    int len = (int)height.size();
    if (len <= 1) {
        return res;
    }
    int l = 0;
    int r = len - 1;
    int l_height = 0;
    int r_height = 0;
    while (l < r) {
        if (height[l] < height[r]) {
            l_height = max(l_height, height[l]);
            res += l_height - height[l];
            l++;
        } else {
            r_height = max(r_height, height[r]);
            res += r_height - height[r];
            r--;
        }
    }
    return res;
}
posted @ 2022-09-26 16:30  天下太平  阅读(14)  评论(0编辑  收藏  举报