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;
}
相关推荐
fen_fen10 分钟前
学习笔记(26):线性代数-张量的降维求和,简单示例
笔记·学习·算法
王禄DUT11 分钟前
炉石传说 第八次CCF-CSP计算机软件能力认证
c++·算法
白熊18837 分钟前
【推荐算法】DeepFM:特征交叉建模的革命性架构
算法·架构·推荐算法
L_cl39 分钟前
【Python 算法零基础 4.排序 ⑪ 十大排序算法总结】
python·算法·排序算法
小刘不想改BUG1 小时前
LeetCode 70 爬楼梯(Java)
java·算法·leetcode
老歌老听老掉牙1 小时前
使用 SymPy 进行向量和矩阵的高级操作
python·线性代数·算法·矩阵·sympy
lifallen1 小时前
Flink checkpoint
java·大数据·算法·flink
比特森林探险记1 小时前
Go 中的 Map 与字符处理指南
c++·算法·golang
安全系统学习3 小时前
网络安全逆向分析之rust逆向技巧
前端·算法·安全·web安全·网络安全·中间件
菜鸟懒懒4 小时前
exp1_code
算法