Trapping Rain Water

Source: Internet
Author: User

Resolution Reference: Http://www.xuebuyuan.com/1586534.html

began to be that to the figure of the cheat, thought as long as the drop up can be, and in fact, some of the middle of the highest point, and can not feel the whole rain height.

Finally, two pointers are traversed in the middle, each moving smaller, and the current maximum is not moving. Calculate the area to use the current second-highest line.

1 classSolution {2  Public:3     intMin (intAintb)4     {5         returnA>b?b:a;6     } One     intTrap (vector<int>&height) { A         intLen =height.size (); -  -         if(len<=2) the            return 0; -int secondheight = 0; -         intI=0, j=len-1, area=0; -          while(i!=j) +         { A            if(Height[j]>secondheight && height[i]>secondheight) at            { -Secondheight =Min (Height[i],height[j]); -            } -  -            if(height[i]>Height[j]) -            { in                if(height[j]<secondheight) -Area + = Secondheight-Height[j]; toj--; +            } -            Else the            { *                if(height[i]<secondheight) $Area + = Secondheight-Height[i];Panax Notoginsengi++; -            } the         } +         returnArea ; A     } the};

Trapping Rain Water

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.