您的位置:首页 > 健康 > 美食 > 【CT】LeetCode手撕—42. 接雨水

【CT】LeetCode手撕—42. 接雨水

2024/10/8 10:52:41 来源:https://blog.csdn.net/weixin_44382896/article/details/139888905  浏览:    关键词:【CT】LeetCode手撕—42. 接雨水

目录

  • 题目
  • 1- 思路
  • 2- 实现
    • ⭐42. 接雨水——题解思路
  • 3- ACM实现

题目

  • 原题连接:42. 接雨水

1- 思路

模式识别:求雨水的面积 ——> 不仅是只求一个比当前元素大的元素,还要求面积

单调栈

  • 应用场景,需要找到左边比当前元素大的元素

单调栈实现

  • 当前元素和栈口元素作比较,如果当前元素大于栈口元素,此时收集结果:
  • 例如 栈口元素是 10,如果当前元素是 30
    • 此时找到 元素 10 右侧第一个比 它大的元素值是 30
    • 右侧第一个比他大的元素是 栈里的第二个元素

单调栈的维护

  • 单调栈与当前元素,存在三种情况,① 等于、②小于、③大于。要用单调栈来存储遍历过的元素
    • 如果小于等于 栈口元素,此时直接入栈
    • 如果大于栈口元素,此时收集结果
      • ①凹槽底部元素:int mid = st.top(); st.pop();
      • ②计算水高:int h = Math.min(st.top(),height[i])-height[mid]; 从右侧柱高,和左侧柱高取个最小值
      • ③计算雨水面积宽度:int width = i - st.pop() - 1;
      • ④计算面积:area = h * width;

2- 实现

⭐42. 接雨水——题解思路

在这里插入图片描述

class Solution {public int trap(int[] height) {int sum = 0;if(height.length == 0){return 0;}// 定义栈Stack<Integer> st = new Stack<Integer>();st.push(0);for(int i = 1 ; i < height.length;i++){if(height[i] <= height[st.peek()]){st.push(i);}else{while(!st.isEmpty() && height[i] > height[st.peek()]){int mid = st.peek();st.pop();if(!st.isEmpty()){int h = Math.min(height[st.peek()],height[i]) - height[mid];int width = i-st.peek() - 1; int hold = h*width;sum+=hold;}}st.push(i);}}return sum;}
}

3- ACM实现

public class getRain {public static int getRain(int[] nums){// 定义单调栈int len = nums.length;if(len==0){return 0;}int sum = 0;Stack<Integer> st = new Stack<>();st.push(0);for(int i = 1 ; i < len;i++){if(nums[i]<=nums[st.peek()]){st.push(i);}else{while(!st.isEmpty() && nums[i] > nums[st.peek()]){int mid = st.peek();st.pop();if(!st.isEmpty()){int h = Math.min(nums[st.peek()],nums[i])-nums[mid];int width = i - st.peek()-1;int hold = h*width;sum+=hold;}}}st.push(i);}return sum;}public static void main(String[] args) {// 计算Scanner sc = new Scanner(System.in);System.out.println("输入数组长度");int n = sc.nextInt();int[] nums = new int[n];for(int i = 0 ; i < n ; i ++){nums[i] = sc.nextInt();}System.out.println("雨水面积是"+getRain(nums));}
}

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com