Leet_Code_42_接雨水

public int trap(int[] height) {
        int result = 0;
        int maxValue = -1;
        int maxAdder = 0;
        //取数组最大的下标和它的值
        for (int i=0;i<height.length;i++) {
            if (height[i] > maxValue) {
                maxValue = height[i];
                maxAdder = i;
            }
        }

        for (int left = 0;left < maxAdder;left ++) {
            for (int i=left + 1;i<=maxAdder;i++) {
                if (height[i] < height[left]) {
                    result = result + (height[left] - height[i]);
                } else {
                    left = i;
                }
            }

        }

        for (int right = height.length-1;right > maxAdder;right --) {
            for (int i=right-1;i>=maxAdder;i--) {
                if (height[i] < height[right]) {
                    result = result + (height[right] - height[i]);
                } else {
                    right = i;
                }
            }
        }
        return result;
    }
原文地址:https://www.cnblogs.com/juniorMa/p/14546374.html