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;
}
相关推荐
想唱rap8 小时前
Linux开发工具(4)
linux·运维·服务器·开发语言·算法
前端炒粉8 小时前
21.搜索二维矩阵 II
前端·javascript·算法·矩阵
星释8 小时前
Rust 练习册 :Rail Fence Cipher与栅栏密码
开发语言·算法·rust
东方隐侠安全团队-千里8 小时前
第3节 RSA算法开启公钥加密时代
网络·人工智能·算法
7澄19 小时前
深入解析 LeetCode 1:两数之和
算法·leetcode·职场和发展·arraylist
Moonbit9 小时前
MGPIC 初赛提交倒计时 4 天!
后端·算法·编程语言
Miraitowa_cheems9 小时前
LeetCode算法日记 - Day 98: 分割回文串 II
数据结构·算法·leetcode·深度优先·动态规划
立志成为大牛的小牛9 小时前
数据结构——三十九、顺序查找(王道408)
数据结构·学习·程序人生·考研·算法
2301_807997389 小时前
代码随想录-day30
数据结构·c++·算法·leetcode