博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Trapping Rain Water 存水问题
阅读量:6696 次
发布时间:2019-06-25

本文共 918 字,大约阅读时间需要 3 分钟。

  hot3.png

输入图片说明

上图索引位置值对应 数组 [0,1,0,2,1,0,1,3,2,1,2,1] 求上图蓝色方格(水滴)数量

public class Solution {    public  int trap(int[] height) {    	if (height == null || height.length<3){    		 return 0;    	}    	    	int max = 0;    	int maxIndex = 0;    	for(int i=0;i
max){ max = height[i]; maxIndex = i; } } int leftMax = 0; int result = 0; for (int i=0;i
=leftMax){ leftMax = height[i]; }else{ result += (leftMax-height[i]); //index索引上的存水量等于 左侧最大值-当前索引位置的值 } } int rightMax = 0; for (int i=height.length-1;i>maxIndex;i--){ if (height[i]>=rightMax){ rightMax = height[i]; }else{ result += (rightMax-height[i]); //index索引上的存水量等于 右侧最大值-当前索引位置的值 } } return result; }}

转载于:https://my.oschina.net/nipin/blog/717906

你可能感兴趣的文章
log日志应用 自定义的log
查看>>
Delphi2010/XE2下隐藏程序系统任务栏的图标
查看>>
文件编码问题
查看>>
linux虚拟机的scsi设备id与盘符不一致问题的解决
查看>>
高薪源于专注和极致!
查看>>
CSVDE批量导入域用户
查看>>
IT168关于敏捷开发采访
查看>>
SystemCenter2012SP1实践(11)VMM的初步使用
查看>>
企业集群平台架构实现与应用实战(HAproxy+keepalived篇)
查看>>
Gartner:2012年北美MSS市场分析
查看>>
我的运维之路(二)
查看>>
世界級VR素材平台的機遇與挑戰
查看>>
ipsec over gRE 详解1
查看>>
路由器利用loopback接口实现物理冗余链路的IPSEC ***
查看>>
在隧道上运行IS-IS 和is-is验证
查看>>
解决centos4不能使用yum的方法
查看>>
我的2015年小结
查看>>
jmeter3.x以上版本安装插件的方法
查看>>
读《做单--成交的秘密》有感
查看>>
Tomcat JDBC pool源码部析
查看>>