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;
}
相关推荐
skytier25 分钟前
Construct内报错和定位解决
算法
skytier30 分钟前
Ascend print数据落盘使用
算法
etcix1 小时前
dmenux.c: integrate dmenu project as one file
c语言·前端·算法
papership1 小时前
【入门级-算法-6、排序算法:选择排序】
数据结构·算法·排序算法
汉克老师1 小时前
第十四届蓝桥杯青少组C++选拔赛[2023.2.12]第二部分编程题(4、最大空白区)
c++·算法·蓝桥杯·蓝桥杯c++·c++蓝桥杯
共享家95271 小时前
优先搜索(DFS)实战
算法·leetcode·深度优先
一只懒洋洋2 小时前
中值滤波、方框滤波、高斯滤波、均值滤波、膨胀、腐蚀、开运算、闭运算
算法·均值算法
shellvon2 小时前
你怎么被识别的?从TLS到Canvas的设备追踪术
后端·算法
薛定谔的算法2 小时前
JavaScript栈的实现与应用:从基础到实战
前端·javascript·算法
羚羊角uou2 小时前
【Linux】匿名管道和进程池
linux·c++·算法