Given n non-negative integers representing an elevation map where the width of each bar is 1, compute how much water it is able to trap after raining.

For example,
Given [0,1,0,2,1,0,1,3,2,1,2,1], return 6.

解题思路:

先找到第一块最高的木板,然后从前向最高木板遍历,接着从后向最高木板遍历,JAVA实现如下:

    static public int trap(int[] height) {int result=0,peak=0,firstMax=0;for(int i=0;i<height.length;i++)if(height[i]>height[firstMax])firstMax=i;for(int i=0;i<firstMax;i++)if(height[i]>height[peak])peak=i;else result+=height[peak]-height[i];peak=height.length-1;for(int i=height.length-1;i>firstMax;i--)if(height[i]>height[peak])peak=i;else result+=height[peak]-height[i];return result;}

转载于:https://www.cnblogs.com/tonyluis/p/4496051.html

Java for LeetCode 042 Trapping Rain Water相关推荐

  1. leetcode#42 Trapping rain water的五种解法详解

    leetcode#42 Trapping rain water 这道题十分有意思,可以用很多方法做出来,每种方法的思想都值得让人细细体会. 42. Trapping Rain Water Given ...

  2. LeetCode 42. Trapping Rain Water 【两种解法】(python排序遍历,C++ STL map存索引,时间复杂度O(nlogn))

    LeetCode 42. Trapping Rain Water Python解法 解题思路: 本思路需找到最高点左右遍历,时间复杂度O(nlogn),以下为向左遍历的过程. 将每一个点的高度和索引存 ...

  3. 【重点:BFS】LeetCode 407. Trapping Rain Water II

    LeetCode 407. Trapping Rain Water II 博客转载自:http://www.cnblogs.com/grandyang/p/5928987.html [太难了,被智商碾 ...

  4. 【重点:DP 双指针 栈】LeetCode 42. Trapping Rain Water

    LeetCode 42. Trapping Rain Water 本博客转载自:http://www.cnblogs.com/grandyang/p/4402392.html [自己又不会做,抄的-& ...

  5. LeetCode 42 Trapping Rain Water 收集雨水

    LeetCode 42 Trapping Rain Water 收集雨水 Given n non-negative integers representing an elevation map whe ...

  6. Leetcode 407. Trapping Rain Water II 收集雨水2 解题报告

    1 解题思想 我看了下题目,发现比预想中的简单,加之我比较烂,所以其实我还是没做,只是看懂了上回贴的代码,然后做了一下注释,现在我来讲下题目. 首先请看下上一题,上一题是2D的这题是3D的: Leet ...

  7. Leetcode 动态规划 Trapping Rain Water

    本文为senlie原创.转载请保留此地址:http://blog.csdn.net/zhengsenlie Trapping Rain Water Total Accepted: 14568 Tota ...

  8. LeetCode 42. Trapping Rain Water(收集雨水Ⅰ)

    题目描述: Given n non-negative integers representing an elevation map where the width of each bar is 1, ...

  9. [LeetCode]42. Trapping Rain Water雨水填坑

    这个题难点在于无法保证右边是不是有更高的墙可以保证挡住水 双指针可以解决 /*两边指针保证,保证另外一边肯定有能挡住水的地方.如果从一边开始,不考虑另一边,是无法保证右边肯定有挡水的墙,如果右边只都比 ...

最新文章

  1. LSM 自适应信号处理代码
  2. python3.7 keras和tensorflow兼容_结果无法在Python中用Keras和TensorFlow重现
  3. 垃圾回收策略和算法,看这篇就够了
  4. 还在用Postman?来,花2分钟体验下ApiPost的魅力!
  5. 【翻译】InterlockedIncrement内部是如何实现的?
  6. linux-soft-yum源
  7. 机器视觉工业光源知识总结
  8. 城市轨道交通工程毕业论文题目
  9. 软件设计师中级考试备考资料
  10. BZOJ系列1406《[AHOI2007]密码箱》题解
  11. 听歌什么蓝牙耳机好用?听歌比较好的蓝牙耳机推荐
  12. JAVA在线考试管理系统(源代码+论文+开题报告+外文翻译+英文文献+答辩PPT)
  13. JAVA音视频解决方案----视频基础知识
  14. Kotlin学习(五)之----集合 + 数组类型 + 解构写法 list,set,map.....
  15. 点击率(CTR)平滑手段
  16. 大自然保护协会2018全球影赛获奖作品合集
  17. 国外创意名片设计欣赏的200佳网站推荐(系列十三)
  18. python如何分析高频词_python数据分析高频词提取,pyecharts词云制作并保存
  19. 电磁阀工作原理及控制电路设计
  20. 用Eicon接口卡来连接帧中继线路(转)

热门文章

  1. 「BZOJ2654」tree
  2. 个人的博客搭建(持续更新)
  3. 在caffe中使用hdf5的数据
  4. 领域驱动设计和开发实战总结
  5. b/s c/s结构的区别!
  6. 如何给微软提反馈建议以及bug
  7. eclipse编辑js卡死解决方案
  8. RabbitMQ简单介绍+Windows环境安装
  9. Hibernate 补充 ManyToOne、OneToMany、OneToOne的使用例
  10. Android 集成微信分享2,实现微信分享