leetcode42接雨水问题

接雨水

题目描述

题目分析

核心思想:

代码

java版本:

java 复制代码
package com.pxx.leetcode.trapRainWaterDoublePoniter;

public class Solution1 {
    public int trap(int[] height) {
        if (height.length == 0) {
            return 0;
        }
        int n = height.length;
        int left = 0;
        int right = n - 1;

        int leftMax = height[0];
        int rightMax = height[n - 1];

        int ans = 0;//保留水通的总数的

        //边走边计算每一个位置
        while (left <= right) {
            leftMax = Math.max(leftMax, height[left]);
            rightMax = Math.max(rightMax, height[right]);

            //刚开始两个相等
            //我们的目的是找到一个最小值,然后减去当前的水通高度
            //数据分析:4 2 0 3 2 5
            //leftMax = height[0] = 4;
            //rightMax = height[5] = 5;
            //选择leftMax 因为它小,所以 ans = 4 - 4 = 0;//当前水通单位为0
            //既然leftMax被占据了之后,那么它就要++,因为这个桶的位置已经被计算完了left++
            //结束之后leftMax与rightMax已经别保留了下来,也就是leftMax = 4,rightMax = 5;
            //---------------------------进入循环(left(1) <= right(5))------------------------------------
            //判定左边最大值max(4(被上次保留的最大值),当前桶的高度=2)->leftmax = 4;
            //判定右边最大值max(5, 5)->rightMax = 5
            //这个时候是leftMax=4, 因为leftMax比较小,ans = 0(上面的水通单位,后面类推) + (4 - 2(当前水通高度) = 2)
            //上面就计算出当前水通能存储的水量是2 ans = 0+2;//目前占两个水通单位
            //保留最大值与最小值,杀入下一次循环里面,因为left又被占了,所以left又要++,right依旧保持不动
            //---------------------------进入循环(left(2) <= right(5))--------------------------------------
            //max(4, 0)->leftmax = 4
            //max(5, 5)->rightmax = 5
            //还是左边比较小,那么又杀入左边
            //ans = 2(上面的水桶数) + (4)(4-height[i(2)=0,4-0]) = 6;
            //--------------------------
            //改变情况简单分析
            //一旦左边leftMax > rightMax
            //就要从右边开始轮替占位了
            //核心思想:哪边小,哪边站位,哪边轮替
            if (leftMax < rightMax) {
                ans += leftMax - height[left];
                left++;
            } else {
                ans += rightMax - height[right];
                right--;
            }
        }
        return ans;
    }
}

c语言版本:

cpp 复制代码
#include <stdio.h>

int trap(int height[], int n) {
    if (n == 0) {
        return 0;
    }

    int left = 0;
    int right = n - 1;

    int leftMax = height[0];
    int rightMax = height[n - 1];

    int ans = 0;

    while (left <= right) {
        leftMax = leftMax > height[left] ? leftMax : height[left];
        rightMax = rightMax > height[right] ? rightMax : height[right];

        if (leftMax < rightMax) {
            ans += leftMax - height[left];
            left++;
        } else {
            ans += rightMax - height[right];
            right--;
        }
    }
    return ans;
}

int main() {
    // 示例用法
    int height[] = {4, 2, 0, 3, 2, 5};
    int n = sizeof(height) / sizeof(height[0]);

    int result = trap(height, n);

    printf("存储的水量是:%d\n", result);

    return 0;
}
相关推荐
_殊途1 小时前
《Java HashMap底层原理全解析(源码+性能+面试)》
java·数据结构·算法
珊瑚里的鱼4 小时前
LeetCode 692题解 | 前K个高频单词
开发语言·c++·算法·leetcode·职场和发展·学习方法
秋说5 小时前
【PTA数据结构 | C语言版】顺序队列的3个操作
c语言·数据结构·算法
lifallen6 小时前
Kafka 时间轮深度解析:如何O(1)处理定时任务
java·数据结构·分布式·后端·算法·kafka
liupenglove6 小时前
自动驾驶数据仓库:时间片合并算法。
大数据·数据仓库·算法·elasticsearch·自动驾驶
python_tty7 小时前
排序算法(二):插入排序
算法·排序算法
然我7 小时前
面试官:如何判断元素是否出现过?我:三种哈希方法任你选
前端·javascript·算法
F_D_Z8 小时前
【EM算法】三硬币模型
算法·机器学习·概率论·em算法·极大似然估计
秋说8 小时前
【PTA数据结构 | C语言版】字符串插入操作(不限长)
c语言·数据结构·算法
凌肖战9 小时前
力扣网编程135题:分发糖果(贪心算法)
算法·leetcode