leetcode-42. Trapping Rain Water 题目: 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 examp
题目连接:Leetcode 042 Trapping Rain Water 解题思路:从左向右遍历一遍,保存每个位置往左的最高值。再从右往左遍历一遍,保存每个位置往右的最高值。最后遍历一遍数组,取左右最高值中较小的一个,减去当前值,即为这个位置增加的量。 class Solution {public:int trap(vector<int>& height) {int n = height.s
题目: 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,
一.题目: 给定一个m x n的矩阵,其中的值均为正整数,代表二维高度图每个单元的高度,请计算图中形状最多能接多少体积的雨水。 二.解题思路: 要想知道每个位置的盛水量,我们需要计算它的四邻域高度的最小值.另外,注意到里面位置的盛水量和外围的高度值有关系,所以我们可以从边界开始一层一层往里计算,使用广度优先搜索不断更新每个位置的高度值. 代码如下: class Solution(object)
LeetCode:Container With Most Water,Trapping Rain Water Container With Most Water 题目链接 Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). n vert
无脑写代码代码(TLE): 1 #include<iostream> 2 #include<vector> 3 4 using namespace std; 5 6 int findLeft(int i, vector<int> height) 7 { 8 int t = i-1; 9 while ((t+1<height.size())&&t >=
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], re