一、题目
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/e5de64f32489ed4b727797b8a2166ec7.png)
二、代码
class Solution
{
public int trap(int[] height)
{
int i,j;
int length = height.length;
int return_sum = 0 ;
int[] left_height = new int[length];
int[] right_height = new int[length];
left_height[0] = height[0];
right_height[length - 1 ] = height[length - 1];
for(i=1;i<length;i++)
{
left_height[i] = Math.max(left_height[i-1],height[i]);
}
for(i=length-2;i>=0;i--)
{
right_height[i] = Math.max(right_height[i+1],height[i]);
}
for(i=0;i<length;i++)
{
return_sum +=Math.min(left_height[i],right_height[i]) - height[i];
}
return return_sum;
}
}
三、运行结果
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/63bb53a46e6696845f3c5b51ad94f3cf.png)