leetcode-42
代码如下,使用语言C
使用算法,暴力算法
int trap(int* height, int heightSize){
int i,sum=0;
for(i=1;i<heightSize-1;i++)
{
int j,leftmax=i-1,leftmaxheight=height[i-1],rightmax=i+1,rightmaxheight=height[i+1];
for(j=i-1;j>=0;j--)
{
if(height[j]>leftmaxheight)
{
leftmax=j;
leftmaxheight=height[j];
}
}
for(j=i+1;j<heightSize;j++)
{
if(height[j]>rightmaxheight)
{
rightmax=j;
rightmaxheight=height[j];
}
}
if(leftmaxheight>height[i]&&rightmaxheight>height[i])
{
if(leftmaxheight>rightmaxheight)
sum=sum+(rightmaxheight-height[i]);
else
sum=sum+(leftmaxheight-he