码迷,mamicode.com
首页 > 移动开发 > 详细

42. Trapping Rain Water

时间:2017-09-24 14:29:41      阅读:205      评论:0      收藏:0      [点我收藏+]

标签:span   ping   style   while   height   blog   else   ret   class   

class Solution {
    public int trap(int[] height) {
        int sum=0;
        int l=0;
        int r=height.length-1;
        int left=0;
        int right=0;
        while(l<=r)
        {
            if(left<=right)
            {
                if(height[l]>left)
                    left=height[l];
                else
                    sum+=left-height[l];
                l++;
            }
            else
            {
                if(height[r]>right)
                    right=height[r];
                else
                    sum+=right-height[r];
                r--;
            }
        }
        return sum;
    }
}

 

42. Trapping Rain Water

标签:span   ping   style   while   height   blog   else   ret   class   

原文地址:http://www.cnblogs.com/asuran/p/7586972.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!