- 浏览: 184658 次
- 性别:
- 来自: 济南
文章分类
最新评论
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.
The above elevation map is represented by array [0,1,0,2,1,0,1,3,2,1,2,1]. In this case, 6 units of rain water (blue section) are being trapped. Thanks Marcos for contributing this image!
让我们计算出存水的总量,如果从左边依次计算,计算当前最高点和最低点之间的存水量,这样计算的结果小于实际的存水量,因为当前的最底点可能会被淹没(如果它两边有比它高的,它就会被淹没)。我们可以采用两个指针,从两边开始,找到一个最高点,然后再找到一个次高点,保持最高点不动,只移动次高点,次高点减去移动经过的柱子就是总的存水量。代码如下:
For example,
Given [0,1,0,2,1,0,1,3,2,1,2,1], return 6.
The above elevation map is represented by array [0,1,0,2,1,0,1,3,2,1,2,1]. In this case, 6 units of rain water (blue section) are being trapped. Thanks Marcos for contributing this image!
让我们计算出存水的总量,如果从左边依次计算,计算当前最高点和最低点之间的存水量,这样计算的结果小于实际的存水量,因为当前的最底点可能会被淹没(如果它两边有比它高的,它就会被淹没)。我们可以采用两个指针,从两边开始,找到一个最高点,然后再找到一个次高点,保持最高点不动,只移动次高点,次高点减去移动经过的柱子就是总的存水量。代码如下:
public class Solution { public int trap(int[] height) { if(height == null || height.length < 3) return 0; int left = 0; int right = height.length - 1; int secondMaxHeight = Integer.MIN_VALUE; int area = 0; while(left < right) { if(height[left] < height[right]) { secondMaxHeight = Math.max(secondMaxHeight, height[left]); area += secondMaxHeight - height[left]; left ++; } else { secondMaxHeight = Math.max(secondMaxHeight, height[right]); area += secondMaxHeight - height[right]; right --; } } return area; } }
发表评论
-
498. Diagonal Traverse
2019-11-15 13:52 268Given a matrix of M x N eleme ... -
496 Next Greater Element I
2019-11-14 13:50 271You are given two arrays (witho ... -
Word Break II
2016-03-09 03:15 388Given a string s and a dictiona ... -
Insert Interval
2016-03-08 02:11 378Given a set of non-overlapping ... -
Merge Intervals
2016-03-07 05:25 503Given a collection of intervals ... -
Merge k Sorted Lists
2016-03-07 04:03 567Merge k sorted linked lists and ... -
Multiply Strings
2016-03-06 07:27 481Given two numbers represented a ... -
N-Queens II
2016-03-06 03:06 666Follow up for N-Queens problem. ... -
N-Queens
2016-03-06 02:47 472The n-queens puzzle is the prob ... -
First Missing Positive
2016-03-05 03:09 432Given an unsorted integer array ... -
Spiral Matrix
2016-03-04 03:39 582Given a matrix of m x n element ... -
Repeated DNA Sequences
2016-03-03 03:10 428All DNA is composed of a series ... -
Increasing Triplet Subsequence
2016-03-02 02:48 903Given an unsorted array return ... -
Maximum Product of Word Lengths
2016-03-02 01:56 933Given a string array words, fin ... -
LRU Cache
2016-02-29 10:37 606Design and implement a data str ... -
Super Ugly Number
2016-02-29 07:07 690Write a program to find the nth ... -
Longest Increasing Path in a Matrix
2016-02-29 05:56 855Given an integer matrix, find t ... -
Coin Change
2016-02-29 04:39 788You are given coins of differen ... -
Minimum Height Trees
2016-02-29 04:11 721For a undirected graph with tre ... -
Bulb Switcher
2016-02-28 12:12 441There are n bulbs that are init ...
相关推荐
js js_leetcode题解之42-trapping-rain-water.js
c语言入门 C语言_leetcode题解之42-trapping-rain-water.c
Trapping Rain Water 刷题顺序 刷题内容总的来说包括数据结构、算法和技巧 按照tag分类别进行刷题,跳过like<200>like的题目 按Acceptance由高到低进行,每个tag所刷题目大于20(对于类型较少的题目大于5),则进入下...
在本压缩包中,我们关注的是一个Python编程相关的学习资源,特别针对LeetCode平台上的第42题“接雨水”(Trapping Rain Water)的面试题解。LeetCode是一个广泛被程序员用来提升算法技能和准备面试的在线平台,其中...
接下来,我们转向LeetCode的第42题“接雨水”(Trapping Rain Water)。这是一道典型的二维数组问题,考察的是动态规划和双指针技巧。 题目描述:给定一个高度不一的数组代表地面,雨水会积累在低点。求能容纳的...
Trapping Rain Water LeetCode 61 RotateList LeetCode 75 Sort Colors LeetCode 125 Valid Palindrome LeetCode 167 Two Sum II - Input array is sorted LeetCode 344 Reverse String LeetCode 345 Reverse Vowels...
5. 字符串处理问题:例如Candy(糖果)、Trapping Rain Water(接雨水)、Reverse Words in a String II(翻转字符串中的单词)等,考察候选人对字符串操作的熟练度。 6. 动态规划问题:如Minimum Size Subarray ...
Trapping Rain Water 85. Maximal Rectangle Monotonic stack for 2d array 239. Sliding Window Maximum 255. Verify Preorder Sequence in Binary Search Tree 907. Sum of Subarray Minimums 二叉搜索树 99. ...
Trapping Rain Water II], BFS/Priority queue 2017.06.19 打卡[LeetCode 145. Binary Tree Postorder Traversal], Tree/stack 2017.06.20 打卡[LeetCode 107. Binary Tree Level Order Traversal II], Tree/BFS ...
力码解决方案 Leetcode是一个网站,人们——...├── Trapping Rain Water │ ├── Readme.md │ └── solution.js ├── Wildcard Matching │ ├── Readme.md │ └── solution.js ├── Valid Number │
- **2.1.15 Trapping Rain Water** - 计算雨水能够困住的水量。 - 实现思路:使用动态规划方法,记录每条柱子左侧最高值和右侧最高值,然后计算水量。 - **2.1.16 Rotate Image** - 将一个正方形矩阵顺时针旋转...
- Trapping Rain Water: 给定一个数组,其中每个元素代表一个宽度为1的柱子高度,计算能接多少雨水。 - Multiply Strings: 给定两个非负整数字符串形式的数,要求进行乘法运算。 - Wildcard Matching: 实现支持'?'和...
Trapping Rain Water Integer to English Words Regular Expression Matching Merge K Sorted Lists Remove Invalid Parentheses Serialize and Deserialize Binary Tree Minimum Window Substring C++ STL中常见...
Trapping Rain Water **知识点:** - **问题描述:** - 给定一个数组表示高度,计算能储存多少雨水。 - **解决方案分析:** - **动态规划:** - 计算每个位置左边最高和右边最高的值。 - 遍历数组,对于每个...
...The number of questions is increasing recently. Here is the classification of all `468` questions. ...I'll keep updating for full summary and better solutions....|-----|---------------- | --------------- |...
二、trapping rain water 雨水收集问题 问题描述 给定一个 number 组成的数组,值代表箱子的高度,下雨后,这些箱子中的间隙一共能收集到多少雨水 解答 暴力破解: 遍历数组,找当前 index 往左的最大值和当前 index...
其中第42题,"接雨水"(Trapping Rain Water)是一道经典的二维数组处理问题,涉及到深度优先搜索(DFS)、广度优先搜索(BFS)、栈、队列以及动态规划等算法知识。在这个问题中,我们被要求计算一个二维数组表示的...
多线程 leetcode 前言 每天刷点leetcode,基于java语言实现。 leetcode上难度分三档:easy,medium,hard. 如下: easy medium Remove Nth Node From End of List Swap Nodes ...Trapping Rain Water