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