Trapping Rain Water

链接

class Solution {
    public int trap(int[] height) {
        int left = 0, right = height.length-1;
        int res = 0;
        // 左边大于右边,说明走过头了
        if(left>=right) return res;
        
        int leftheight = height[left];
        int rightheight = height[right];

        //two pointers问题,那边矮,移动哪边
        while(left heights[left]){
                    res += (leftheight -heights[left]);
                }else{
                    leftheight = height[left];
                }
            }else{
                right --;
                if(rightheight>heights[right]){
                    res += (rightheight-heights[right]);
                }else{
                    rightheight = heights[right];
                }
            }
        }
        return res;
    }
}

你可能感兴趣的:(Trapping Rain Water)