刷题42. Trapping Rain Water

一、题目说明

题目是42. Trapping Rain Water,翻译起来就是“接雨水”。给n个非负正数代表高度,每个正数宽度为1,让计算能多少雨水。题目难度是Hard

二、我的解法

这个题目是找“坑”,然后计算里面可以存的“雨”。总共提交了5次,前面几次都是边界错误。

代码如下:

#include
#include

using namespace std;
class Solution{
    public:
        int trap(vector& height){
            if(height.size()<1) return 0;
            int len = height.size();
            int sum = 0,area=0,h;
            bool lflag = false,rflag = false;
            
            int left=0,leftStart,right,rightEnd=len-1,mid;
            
            while(leftleft && height[right]<=height[right-1]){
                    right--;
                }
                rightEnd = right;
                
                if(height[rightEnd]<=height[left]){
                    right = rightEnd;
                    //降 
                    while(right>left && (height[right]<=height[rightEnd])){
                        right--;
                    }
                    //升
                    while(right>left && (height[right]height[t]){
                            area = area + (h-height[t]);
                        }
                    }
                    
                    sum += area;
                    rightEnd = right;
                }else{
                    leftStart = left;
                    //降 
                    while(leftheight[t]){
                            area = area + (h-height[t]);
                        }
                    }
                    
                    sum += area;
                    leftStart = left;
                }
            }
            
            return sum;
        }
};
int main(){
    Solution s;
    vector r;
    r = {0,1,0,2,1,0,1,3,2,1,2,1};
    cout<

性能如下:

Runtime: 8 ms, faster than 61.40% of C++ online submissions for Trapping Rain Water.
Memory Usage: 9.1 MB, less than 91.14% of C++ online submissions for Trapping Rain Water.

三、优化措施

代码虽然正确,但看起来很难过!多番寻找,相对优雅的代码如下:

class Solution{
    public:
        //left、right 
        int trap(vector& height) {
            int n = height.size();
            int lhigh = 0, rhigh = n-1;
            int diff = 0;
    
            // scan from left to right
            for(int i = lhigh; i=lhigh; i--)
            {
                if (height[i] < height[rhigh]) continue;
                for (int j = i+1; j

性能虽然差点,但可读性好多了。

Runtime: 12 ms, faster than 17.25% of C++ online submissions for Trapping Rain Water.
Memory Usage: 9 MB, less than 94.94% of C++ online submissions for Trapping Rain Water.

你可能感兴趣的:(刷题42. Trapping Rain Water)