牛牛有一个围栏,围栏由一系列垂直放置的木板组成,每个木板的宽度都为1。牛牛想知道下雨后,围栏能够存储多少雨水。假设围栏的高度由一个非负整数数组表示,数组中的每个元素代表相应位置的木板高度。请你编写一个程序,计算围栏能够存储的雨水量。
示例1
输入
[0, 1, 0, 2, 1, 0, 1, 3, 2, 1, 2, 1]
输出
6
示例2
输入
[4, 2, 0, 3, 2, 5]
输出
9
备注:
1 0
加载中...
import java.util.*; public class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param fence int整型一维数组 * @return int整型 */ public int trapRainWater (int[] fence) { // write code here } }
class Solution { public: /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param fence int整型vector * @return int整型 */ int trapRainWater(vector
& fence) { // write code here } };
#coding:utf-8 # # 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 # # # @param fence int整型一维数组 # @return int整型 # class Solution: def trapRainWater(self , fence ): # write code here
using System; using System.Collections.Generic; class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param fence int整型一维数组 * @return int整型 */ public int trapRainWater (List
fence) { // write code here } }
/** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param fence int整型一维数组 * @return int整型 */ function trapRainWater( fence ) { // write code here } module.exports = { trapRainWater : trapRainWater };
# # 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 # # # @param fence int整型一维数组 # @return int整型 # class Solution: def trapRainWater(self , fence: List[int]) -> int: # write code here
package main import "fmt" /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param fence int整型一维数组 * @return int整型 */ func trapRainWater( fence []int ) int { // write code here }
/** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param fence int整型一维数组 * @param fenceLen int fence数组长度 * @return int整型 */ int trapRainWater(int* fence, int fenceLen ) { // write code here }
# # 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 # # # @param fence int整型一维数组 # @return int整型 # class Solution def trapRainWater(fence) # write code here end end
object Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param fence int整型一维数组 * @return int整型 */ def trapRainWater(fence: Array[Int]): Int = { // write code here } }
object Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param fence int整型一维数组 * @return int整型 */ fun trapRainWater(fence: IntArray): Int { // write code here } }
import java.util.*; public class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param fence int整型一维数组 * @return int整型 */ public int trapRainWater (int[] fence) { // write code here } }
/** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param fence int整型一维数组 * @return int整型 */ export function trapRainWater(fence: number[]): number { // write code here }
public class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param fence int整型一维数组 * @return int整型 */ func trapRainWater ( _ fence: [Int]) -> Int { // write code here } }
struct Solution{ } impl Solution { fn new() -> Self { Solution{} } /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param fence int整型一维数组 * @return int整型 */ pub fn trapRainWater(&self, fence: Vec
) -> i32 { // write code here } }
[0, 1, 0, 2, 1, 0, 1, 3, 2, 1, 2, 1]
6
[4, 2, 0, 3, 2, 5]
9