LeetCode:接雨水【42】
LeetCode:接雨水【42】
题目描述
给定 n 个非负整数表示每个宽度为 1 的柱子的高度图,计算按此排列的柱子,下雨之后能接多少雨水。

上面是由数组 [0,1,0,2,1,0,1,3,2,1,2,1] 表示的高度图,在这种情况下,可以接 6 个单位的雨水(蓝色部分表示雨水)。 感谢 Marcos 贡献此图。
示例:
输入: [0,1,0,2,1,0,1,3,2,1,2,1]
输出: 6
题目分析
简要思路
- 找出最高点
- 分别从两边往最高点遍历:如果下一个数比当前数小,说明可以接到水
视频分析
Java题解
class Solution {
public int trap(int[] height) {
int result = 0;
int maxValue = -1;
int maxAddr = 0;
for(int i = 0;i<height.length;i++){
if(height[i]>=maxValue){
maxValue = height[i];maxAddr=i;
}
}
//左半部分处理
for(int left = 0;left<maxAddr;left++){
for(int i =left+1;i<=maxAddr;i++){
if(height[i]<height[left])
result=result+(height[left]-height[i]);
else
left = i;
}
}
//右半部分处理
for(int right =height.length-1;right>maxAddr;right--){
for(int i =right-1;i>=maxAddr;i--){
if(height[i]<height[right])
result=result+(height[right]-height[i]);
else
right = i;
}
}
return result;
}
}
LeetCode:接雨水【42】的更多相关文章
- 【完虐算法】LeetCode 接雨水问题,全复盘
大家好! 动态规划题目是总结的比较完整了.下面是自从和大家刷开题总结的动态规划解题方法. 今年全国夏天雨是真的多,突然想到今年北京的夏天也不像往年那么热.不知不觉就稳稳地度过了夏天来到秋天. 恰巧前几 ...
- LeetCode.接雨水
题外话:LeetCode上一个测试用例总是通不过(我在文章末贴出通不过的测试用例),给的原因是超出运行时间,我拿那个测试用例试了下2.037ms运行完.我自己强行给加了这句: && m ...
- leetcode面试题42. 连续子数组的最大和
总结一道leetcode上的高频题,反反复复遇到了好多次,特别适合作为一道动态规划入门题,本文将详细的从读题开始,介绍解题思路. 题目描述示例动态规划分析代码结果 题目 面试题42. 连续子数 ...
- [LeetCode] 接雨水,题 Trapping Rain Water
这题放上来是因为自己第一回见到这种题,觉得它好玩儿 =) Trapping Rain Water Given n non-negative integers representing an eleva ...
- C++ leetcode接雨水
双指针算法"接雨水" 链接:https://leetcode-cn.com/problems/trapping-rain-water/ 给定 n 个非负整数表示每个宽度为 1 的柱 ...
- 【sql】leetcode习题 (共 42 题)
[175]Combine Two Tables (2018年11月23日,开始集中review基础) Table: Person +-------------+---------+ | Column ...
- 648. Replace Words
Problem statement In English, we have a concept called root, which can be followed by some other wor ...
- [LeetCode] 42. Trapping Rain Water 收集雨水
Given n non-negative integers representing an elevation map where the width of each bar is 1, comput ...
- Java实现 LeetCode 42 接雨水
42. 接雨水 给定 n 个非负整数表示每个宽度为 1 的柱子的高度图,计算按此排列的柱子,下雨之后能接多少雨水. 上面是由数组 [0,1,0,2,1,0,1,3,2,1,2,1] 表示的高度图,在这 ...
随机推荐
- Codeforces G. Nick and Array(贪心)
题目描述: Nick had received an awesome array of integers a=[a1,a2,…,an] as a gift for his 5 birthday fro ...
- python开发应用之-时间戳
golang 获取时间戳用time.Now().Unix(),格式化时间用t.Format,解析时间用time.Parse package main import ( "fmt" ...
- Python语言程序设计(3)--字符串类型及操作--time库进度条
1.字符串类型的表示: 三引号可做注释,注释其实也是字符串 2.字符串的操作符 3.字符串处理函数 输出:
- IntelliJ IDEA 2019.2破解
IntelliJ IDEA 2019.2破解 我是参考这个激活的,使用的激活码的方式,需要在百度云盘下载压缩包 https://zhile.io/2018/08/25/jetbrains-licens ...
- L1025
1,对于搜索,我有一个不成熟的想法,这不就是,强化版的for循环吗? 2,反正是搜索,那就先找搜索状态, n,x,n是第几次分,x是分剩下的数. 3,这个我觉得自己努力努力可能可以做出来. 4,首先要 ...
- Tensorflow细节-P319-使用GPU基本的操作
如果什么都不加,直接运行装了GPU的Tensorflow,结果是这样子的 import tensorflow as tf a = tf.constant([1.0, 2.0, 3.0], shape= ...
- python操作json文件获取内容
写case时,将case 写到json文件比写到,写python一定要学会处理json 以下,是要处理的json 处理操作包括:打开json文件,获取json文件内容,关闭json文件,读取内容中的对 ...
- php for循环遍历索引数组
遍历二字,从字面解释就是一个接一个全读访问一次,显示出来. 因为for循环是一个单纯的计数型循环,而索引数组的下标为整型的数值.因此,我们可以通过for循环来遍历索引数组. 我们知道索引数组下标为整型 ...
- c++合并两个序列函数merge()和inplace_merge()
大家在写归并排序时是不是觉得合并两个序列有点麻烦,有快速的方法吗? 我们全部函数自己写,比如: #include<bits/stdc++.h> using namespace std; # ...
- Tex家族关系
小书匠 声明:文章自一份其实很短的 LaTeX 入门文档学习,整理所得. Tex家族关系 Tex家族关系图 1.排版引擎 1.所谓的引擎,是指能够实现断行.分页等操作的程序(请注意这并不是定义) 2. ...