JAVA学习-练习试用Java实现“接雨水”

问题:

给定 n 个非负整数表示每个宽度为 1 的柱子的高度图,计算按此排列的柱子,下雨之后能接多少雨水。

示例 1:

输入:height = [0,1,0,2,1,0,1,3,2,1,2,1]
输出:6
解释:上面是由数组 [0,1,0,2,1,0,1,3,2,1,2,1] 表示的高度图,在这种情况下,可以接 6 个单位的雨水(蓝色部分表示雨水)。 
示例 2:

输入:height = [4,2,0,3,2,5]
输出:9
提示:

n == height.length
0 <= n <= 3 * 104
0 <= height[i] <= 105
以下程序实现了这一功能,请你填补空白处内容:

class Solution(object):
    def trap(self, height):
        ls = len(height)
        if ls == 0:
            return 0
        res, left = 0, 0
        while left < ls and height[left] == 0:
            left += 1
        pos = left + 1
        while pos < ls:
            if height[pos] >= height[left]:
                res += self.rain_water(height, left, pos)
                left = pos
                pos += 1
            elif pos == ls - 1:
                max_value, max_index = 0, pos
                for index in range(left + 1, ls):
                    if height[index] > max_value:
                        max_value = height[index]
                        max_index = index
                res += self.rain_water(height, left, max_index)
                left = max_index
                pos = left + 1
            else:
                pos += 1
        return res
    def rain_water(self, height, start, end):
        if end - start <= 1:
            return 0
        min_m = min(height[start], height[end])
        _________________________;
        step = 0
        for index in range(start + 1, end):
            if height[index] > 0:
                step += height[index]
        return res - step
if __name__ == '__main__':
    s = Solution()
    print (s.trap([2,6,3,8,2,7,2,5,0]))
 

解答思路:

以下是填补空白处后的代码:

class Solution {
    public int trap(int[] height) {
        int ls = height.length;
        if (ls == 0) {
            return 0;
        }
        int res = 0, left = 0;
        while (left < ls && height[left] == 0) {
            left++;
        }
        int pos = left + 1;
        while (pos < ls) {
            if (height[pos] >= height[left]) {
                res += rainWater(height, left, pos);
                left = pos;
                pos++;
            } else if (pos == ls - 1) {
                int maxValue = 0, maxIndex = pos;
                for (int index = left + 1; index < ls; index++) {
                    if (height[index] > maxValue) {
                        maxValue = height[index];
                        maxIndex = index;
                    }
                }
                res += rainWater(height, left, maxIndex);
                left = maxIndex;
                pos = left + 1;
            } else {
                pos++;
            }
        }
        return res;
    }

    public int rainWater(int[] height, int start, int end) {
        if (end - start <= 1) {
            return 0;
        }
        int minM = Math.min(height[start], height[end]);
        int res = (end - start - 1) * minM;
        int step = 0;
        for (int index = start + 1; index < end; index++) {
            if (height[index] > 0) {
                step += height[index];
            }
        }
        return res - step;
    }

    public static void main(String[] args) {
        Solution s = new Solution();
        int[] height = {2, 6, 3, 8, 2, 7, 2, 5, 0};
        System.out.println(s.trap(height));
    }
}

在这段代码中,我们使用双指针法来计算接雨水的量。首先,我们找到第一个不为 0 的柱子,并将其作为左指针。然后,我们从左指针的下一个位置开始,向右移动右指针,直到找到一个比左指针高的柱子。在这个过程中,我们计算每个柱子之间的雨水量,并将其累加到结果中。如果右指针到达了数组的末尾,我们需要找到从左指针到数组末尾中最高的柱子,并将其作为新的右指针,继续计算雨水量。最后,我们返回计算得到的雨水总量。
(文章为作者在学习java过程中的一些个人体会总结和借鉴,如有不当、错误的地方,请各位大佬批评指正,定当努力改正,如有侵权请联系作者删帖。)

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值