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 ...
. 原始題目 給定n個非負整數表示每個寬度為 的柱子的高度圖,計算按此排列的柱子,下雨之后能接多少雨水。 上面是由數組 , , , , , , , , , , , 表示的高度圖,在這種情況下,可以接 個單位的雨水 藍色部分表示雨水 。感謝 Marcos貢獻此圖。 示例: . 思路 最簡單的想法:對於每個元素都要考慮它能接多少雨水: 第一個元素是 ,能接 雨水 第二個元素是 ,能接 雨水 第三個元 ...
2019-04-29 13:12 0 794 推薦指數:
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 ...
這題放上來是因為自己第一回見到這種題,覺得它好玩兒 =) Trapping Rain Water Given n non-negative integers representing an elevation map where the width of each bar ...
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 ...
給定 n 個非負整數表示每個寬度為 1 的柱子的高度圖,計算按此排列的柱子,下雨之后能接多少雨水。 上面是由數組 [0,1,0,2,1,0,1,3,2,1,2,1] 表示的高度圖,在這種情況下,可以接 6 個單位的雨水(藍色部分表示雨水)。 感謝 Marcos 貢獻此圖。 示例: ...
解題思路: 儲水量由最小的一邊決定,我們可以先從左右兩邊同時遍歷,得到最大值,然后分兩種情況處理: 1.只有一個最大值(假設位置為i): 這樣就從左向i遍歷,不斷更新左邊 ...
Given an m x n matrix of positive integers representing the height of each unit cell in a 2D elevation map, compute the volume of water it is able ...
leetcode#42 Trapping rain water 這道題十分有意思,可以用很多方法做出來,每種方法的思想都值得讓人細細體會。 42. Trapping Rain WaterGiven n non-negative integers representing ...
接雨水解法詳解: 題目: 基本思路:從圖上可以看出要想接住雨水,必須是凹字形的,也就是當前位置的左右兩邊必須存在高度大於它的地方,所以我們要想知道當前位置最多能存儲多少水,只需找到左邊最高處max_left和右邊最高處max_right,取他們兩個較小的那邊計算即可(短板效應)。 其實接下 ...