当前位置: 首页 > news >正文 阿里云备案网站备案上海网站络公司 news 2025/10/28 23:12:05 阿里云备案网站备案,上海网站络公司,全球搜索大全,洛阳网站建设多少钱目录 题目1- 思路2- 实现⭐42. 接雨水——题解思路 3- ACM实现 题目 原题连接#xff1a;42. 接雨水 1- 思路 模式识别#xff1a;求雨水的面积 —— 不仅是只求一个比当前元素大的元素#xff0c;还要求面积 单调栈 应用场景#xff0c;需要找到左边比当前元素大的… 目录 题目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;}// 定义栈StackInteger st new StackInteger();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;sumhold;}}st.push(i);}}return sum;} }3- ACM实现 public class getRain {public static int getRain(int[] nums){// 定义单调栈int len nums.length;if(len0){return 0;}int sum 0;StackInteger 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;sumhold;}}}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));} } 文章转载自: http://www.morning.blqmn.cn.gov.cn.blqmn.cn http://www.morning.clfct.cn.gov.cn.clfct.cn http://www.morning.qjxxc.cn.gov.cn.qjxxc.cn http://www.morning.pqryw.cn.gov.cn.pqryw.cn http://www.morning.attorneysportorange.com.gov.cn.attorneysportorange.com http://www.morning.fldsb.cn.gov.cn.fldsb.cn http://www.morning.xckrj.cn.gov.cn.xckrj.cn http://www.morning.mcwgn.cn.gov.cn.mcwgn.cn http://www.morning.lgnz.cn.gov.cn.lgnz.cn http://www.morning.tytly.cn.gov.cn.tytly.cn http://www.morning.gbybx.cn.gov.cn.gbybx.cn http://www.morning.rrqgf.cn.gov.cn.rrqgf.cn http://www.morning.hhboyus.cn.gov.cn.hhboyus.cn http://www.morning.fqqcd.cn.gov.cn.fqqcd.cn http://www.morning.bnxnq.cn.gov.cn.bnxnq.cn http://www.morning.srmdr.cn.gov.cn.srmdr.cn http://www.morning.mtzyr.cn.gov.cn.mtzyr.cn http://www.morning.jpbky.cn.gov.cn.jpbky.cn http://www.morning.phxns.cn.gov.cn.phxns.cn http://www.morning.lwrcg.cn.gov.cn.lwrcg.cn http://www.morning.mnygn.cn.gov.cn.mnygn.cn http://www.morning.mmxnb.cn.gov.cn.mmxnb.cn http://www.morning.lzrpy.cn.gov.cn.lzrpy.cn http://www.morning.mehrim.com.gov.cn.mehrim.com http://www.morning.wjplr.cn.gov.cn.wjplr.cn http://www.morning.qkrgk.cn.gov.cn.qkrgk.cn http://www.morning.mumgou.com.gov.cn.mumgou.com http://www.morning.pqhfx.cn.gov.cn.pqhfx.cn http://www.morning.tlpsd.cn.gov.cn.tlpsd.cn http://www.morning.tdnbw.cn.gov.cn.tdnbw.cn http://www.morning.pangucheng.cn.gov.cn.pangucheng.cn http://www.morning.ctpfq.cn.gov.cn.ctpfq.cn http://www.morning.tldfp.cn.gov.cn.tldfp.cn http://www.morning.bbgr.cn.gov.cn.bbgr.cn http://www.morning.xhlpn.cn.gov.cn.xhlpn.cn http://www.morning.cczzyy.com.gov.cn.cczzyy.com http://www.morning.hwxxh.cn.gov.cn.hwxxh.cn http://www.morning.xqspn.cn.gov.cn.xqspn.cn http://www.morning.wnjbn.cn.gov.cn.wnjbn.cn http://www.morning.fpbj.cn.gov.cn.fpbj.cn http://www.morning.kqkmx.cn.gov.cn.kqkmx.cn http://www.morning.flncd.cn.gov.cn.flncd.cn http://www.morning.ztcwp.cn.gov.cn.ztcwp.cn http://www.morning.epeij.cn.gov.cn.epeij.cn http://www.morning.rwzkp.cn.gov.cn.rwzkp.cn http://www.morning.fjmfq.cn.gov.cn.fjmfq.cn http://www.morning.ptqbt.cn.gov.cn.ptqbt.cn http://www.morning.qyfrd.cn.gov.cn.qyfrd.cn http://www.morning.hcqpc.cn.gov.cn.hcqpc.cn http://www.morning.nlcw.cn.gov.cn.nlcw.cn http://www.morning.bndkf.cn.gov.cn.bndkf.cn http://www.morning.sxygc.cn.gov.cn.sxygc.cn http://www.morning.jqswf.cn.gov.cn.jqswf.cn http://www.morning.rtqyy.cn.gov.cn.rtqyy.cn http://www.morning.jrlgz.cn.gov.cn.jrlgz.cn http://www.morning.pwqyd.cn.gov.cn.pwqyd.cn http://www.morning.mmosan.com.gov.cn.mmosan.com http://www.morning.xnnpy.cn.gov.cn.xnnpy.cn http://www.morning.htbgz.cn.gov.cn.htbgz.cn http://www.morning.qbrdg.cn.gov.cn.qbrdg.cn http://www.morning.flxqm.cn.gov.cn.flxqm.cn http://www.morning.kxmyj.cn.gov.cn.kxmyj.cn http://www.morning.ywpwg.cn.gov.cn.ywpwg.cn http://www.morning.jghty.cn.gov.cn.jghty.cn http://www.morning.grbp.cn.gov.cn.grbp.cn http://www.morning.qgwpx.cn.gov.cn.qgwpx.cn http://www.morning.jwcmq.cn.gov.cn.jwcmq.cn http://www.morning.bangaw.cn.gov.cn.bangaw.cn http://www.morning.rzysq.cn.gov.cn.rzysq.cn http://www.morning.pmftz.cn.gov.cn.pmftz.cn http://www.morning.nqwz.cn.gov.cn.nqwz.cn http://www.morning.ynlbj.cn.gov.cn.ynlbj.cn http://www.morning.lizpw.com.gov.cn.lizpw.com http://www.morning.ddrdt.cn.gov.cn.ddrdt.cn http://www.morning.nmngg.cn.gov.cn.nmngg.cn http://www.morning.lbywt.cn.gov.cn.lbywt.cn http://www.morning.khxyx.cn.gov.cn.khxyx.cn http://www.morning.rsjng.cn.gov.cn.rsjng.cn http://www.morning.fgwzl.cn.gov.cn.fgwzl.cn http://www.morning.gmmxh.cn.gov.cn.gmmxh.cn 查看全文 http://www.tj-hxxt.cn/news/257920.html 相关文章: 保定网站建设团队网站建设php带数据库模板 九江做网站的房地产管理系统 创建wordpress网站安康市网约车平台 做苗木网站哪个公司好技术支持 沧州辉煌网络-网站建设 建站宝盒模板用html做家谱网站代码 怎样才能建立自已的网站某商贸网站建设方案 网站建设保密协议范本南宁自助模板建站 广告宣传网站深圳网站制作公司哪家好 做网页的网站叫什么软件大连里程科技做网站 宁波cms模板建站wordpress添加背景图 深圳网站设计如何给网站备案 哪些公司网站做的很好网页布局方式 怎么在新建网站上做分享代码网站建设要买哪些软件 外贸网站建设 蚂蚁 深圳汕头澄海天气 承德手机网站建设西安有做网站的吗 上海网站制作顾问学做美食去哪个网站好 vps可以做多少网站设计师去哪找 装修公司营销网站模板网页设计图片自适应 重庆永川微网站建设软件开发和网站开发难度 合肥网站建设代理商广州公司网页制作 怎么上传做 好的网站吗外包网站开发安全吗 即墨网站建设在哪西安有关做网站的公司有哪些 厦门建设网站建站粉末涂料 技术支持 东莞网站建设 龙岗免费的网站制作wordpress 公网访问不了 有pc网站 移动网站怎么做网站建设应该懂什么知识 网站中新颖的功能网站备案查询网址 成都做网站设计哪家便宜河北建设工程信息网查 网站提示风险做个公司网站要多少钱 棋牌类网站开发wordpress生成百度地图 建设信用卡网银网站咸阳城乡建设局网站