LeetCode 42. Trapping Rain Water

题目

1A
c++

O(n^2)

class Solution {
public:
    
    int trap(vector& height) {
        
        int ans=0;
        for(int i=1;iheight[i-1])
            {
                int pos=0;
                for(int j=i-2;j>=0;j--)
                {
                    if(height[j+1]

O(n)

class Solution {
public:
    int left[100005];
    int right[100005];
    int trap(vector& height) {
        
        
        int ans=0;
        if(height.size()==0)
            return ans;
        left[0] = height[0];
        for(int i=1;i=0;j--)
        {
            right[j]=max(right[j+1],height[j]);
        }
        
        for(int i=0;i

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