1. 程式人生 > >42. Trapping Rain Water

42. Trapping Rain Water

turn blog length spa trapping cnblogs height water pub

class Solution {
    public int trap(int[] height) {
        int sum=0;
        int l=0;
        int r=height.length-1;
        int left=0;
        int right=0;
        while(l<=r)
        {
            if(left<=right)
            {
                if(height[l]>left)
                    left
=height[l]; else sum+=left-height[l]; l++; } else { if(height[r]>right) right=height[r]; else sum+=right-height[r]; r
--; } } return sum; } }

42. Trapping Rain Water