一、题目

给定 n 个非负整数表示每个宽度为 1 的柱子的高度图,计算按此排列的柱子,下雨之后能接多少雨水。

LeetCode刷题 42. 接雨水

上面是由数组 [0,1,0,2,1,0,1,3,2,1,2,1] 表示的高度图,在这种情况下,可以接 6 个单位的雨水(蓝色部分表示雨水)。 感谢 Marcos 贡献此图。

LeetCode刷题 42. 接雨水

示例:

输入: [0,1,0,2,1,0,1,3,2,1,2,1]
输出: 6

二、第一次成功提交(C)

int minn(int a,int b)
{
    if(a>b)
        return b;
    else
        return a;
}

int rongJi(int *height, int begin, int end)
{
    int hig = minn(height[begin],height[end]);
    int total = 0;
    for(int i = begin+1; i<end;i++)
    {
        total += (hig-height[i]);
    }
    return total;
}

int getMax(int *height, int begin, int end)
{
    int a = begin;
    for(int i = begin;i < end; i++)
    {
    	if(height[a]< height[i])
    		a=i;
    }
    return a;
}

int trap(int* height, int heightSize) 
{
    if(heightSize == 0 || heightSize == 1 || heightSize == 2)
        return 0;
    int total = 0;
    int left = getMax(height, 0, heightSize);
    int right = left;
    int gao;
    
    while(left != 0 || right != (heightSize-1))
    {
    	if(left != 0)
    	{
    		gao = getMax(height,0,left);
    		total += rongJi(height,gao,left);
    		//printf("%d + %d = %d\n==========\n",gao,left,rongJi(height,gao,left));
    		left = gao;
		}
		
		if(right != (heightSize-1))
		{
			gao = getMax(height,right+1,heightSize);
			total += rongJi(height,right,gao);
			//printf("%d + %d = %d\n==========\n",right,gao,rongJi(height,right,gao));
			right = gao;
		}
    }
    return total;
}

本次提交总结:

           1.一直提交不通过的原因是未考虑边界值,在以后的刷题中一定要判断输入为空等边界值的情况。

           2.具体流程如下,写的不是很清晰,以后再改

LeetCode刷题 42. 接雨水

         3.结果如下图所示:

LeetCode刷题 42. 接雨水

三、别人的优质代码分析总结

1.先上代码

int* QuickSort(int* nums, int *seq, int start, int end);
int trap(int* height, int heightSize) {
    int *sortheight = (int*)malloc(sizeof(int) * heightSize);
    int *seq = (int*)malloc(sizeof(int) * heightSize);
    for(int i = 0; i < heightSize; i++)
    {
        seq[i] = i;
        sortheight[i] = height[i];
    }      
    QuickSort(sortheight, seq, 0, heightSize - 1);
    int left = seq[heightSize - 1], right = left, result = 0;
    for(int i = heightSize - 1; i >= 0; i--)
    {
        //计算seq[i] 与left或right之间的坑,在中间的话不管
        if(seq[i] < left)
        {
            for(int j = seq[i] + 1; j <left; j++)
                result += height[seq[i]] - height[j];
            left = seq[i];
        }
        else if(seq[i] > right)
        {
             for(int j = right + 1; j <seq[i]; j++)
                result += height[seq[i]] - height[j];
            right = seq[i];
        }
    }
    return result;
}

int* QuickSort(int* nums, int *seq, int start, int end)
{
    if(start>=end)return;
    int i = start, j = end, temp = nums[start], tempseq = seq[start];
    while(i<j)
    {
        while(nums[j]>=temp && i<j) j--;
        nums[i] = nums[j]; seq[i] = seq[j];
        while(nums[i]<=temp && i<j) i++;
        nums[j] = nums[i]; seq[j] = seq[i];
    }
    nums[i] = temp; seq[i] = tempseq;
    QuickSort(nums, seq, start, i-1);
    QuickSort(nums, seq, i+1, end);
    return;
}

 

相关文章: