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 example,
Given [0,1,0,2,1,0,1,3,2,1,2,1], return 6.

 1 #include<stdio.h>
 2
 3 int trap( int* height, int heightSize ){
 4
 5     int left = 0;
 6     int right = heightSize - 1;
 7     int middle = 0;
 8     int water = 0;
 9
10     while( left < right ){
11
12         if( height[left] < height[right]){
13             middle = middle > height[left] ? middle : height[left];
14             water = water + middle - height[left];
15             left ++;
16         }else{
17             middle = middle > height[right] ? middle : height[right];
18             water = water + middle - height[right];
19             right --;
20         }
21     }
22     printf("Water is %d\n", water);
23     return water;
24 }
25
26 int main(){
27     int height[] = {0,1,0,2,1,0,1,3,2,1,2,1};
28     int heightSize = 12;
29     trap( height, heightSize );
30     return 0;
31 }
时间: 2024-10-23 20:40:02

LeetCode 42的相关文章

LeetCode 42 Trapping Rain Water(积水体积)

题目链接: https://leetcode.com/problems/trapping-rain-water/?tab=Description Problem: 根据所给数组的值,按照上图的示意图.求解积水最大体积. 首先对所给数组进行遍历操作,求出最大高度所对应的下标为maxIndex 之后从左向右进行遍历,设置左边高度为leftMax 并初始化为 height[0],从i==1到i==maxIndex进行遍历.不断更新water,以及leftMax 当height[I]小于leftMax时

[leetcode] 42. 接雨水

42. 接雨水 思路:一块柱子能接水的量取决于它左右两边最高的柱子中较短的一个. class Solution { public int trap(int[] height) { int left[] = new int[height.length]; int right[] = new int[height.length]; int max = 0; for (int i = 0; i < height.length; i++) { max = Math.max(max, height[i])

leetCode(42):Flatten Binary Tree to Linked List

Given a binary tree, flatten it to a linked list in-place. For example, Given 1 / 2 5 / \ 3 4 6 The flattened tree should look like: 1 2 3 4 5 6 If you notice carefully in the flattened tree, each node's right child points to the next node of a pre-o

LeetCode 42. Trapping Rain Water

首先说说最初的但是不正确的思路,stk存值,初始化为输入数组的第一位.从左往右读入数组每一位,如果height[i]>stk.top(),说明stk.top()无法蓄水,pop掉,替换为height[i]:如果height[i]<=stk.top(),那么从i开始遍历,直到找到大于等于stk.top()的height[i]或者直到最后一位,找的时候维护变量tmp存蓄水的量,但是如果没有找到大于等于stk.top()的height[i]的话,实际上是无法蓄水的,也就是tmp是无意义的(因此要另开

leetCode 42.Trapping Rain Water(凹槽的雨水) 解题思路和方法

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 example, Given [0,1,0,2,1,0,1,3,2,1,2,1], return 6. The above elevation map

19.2.4 [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. 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

[LeetCode] 11. Container With Most Water 装最多水的容器

Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of line i is at (i, ai) and (i, 0). Find two lines, which together with x-axis forms a containe

leetcode接雨水问题的总结

题目:leetcode 42题 解法一:动态规划 int trap(vector<int>& height) { int ans = 0; int size = height.size(); if(size) { vector<int> max_left(size); vector<int> max_right(size); max_left[0] = height[0]; max_right[size-1] = height[size-1]; for(int

[Leetcode][Python]42: Trapping Rain Water

# -*- coding: utf8 -*-'''__author__ = '[email protected]' 42: Trapping Rain Waterhttps://oj.leetcode.com/problems/trapping-rain-water/ Given n non-negative integers representing an elevation map where the width of each bar is 1,compute how much water