您的位置:首页 > 移动开发

LeetCode - Trapping Rain Water

2017-12-12 00:00 337 查看
class Solution {
public int maxArea(int[] height) {

int n = height.length;
int left = 0;
int right = n-1;

int leftH = height[left];
int rightH = height[right];

if (n==2) {
return (rightH<leftH)?rightH:leftH;
};

int leftMax = leftH;
int rightMax = rightH;
int res = 0;
while (left < right){
if (leftMax <= rightMax){
left++;
if (height[left]<=leftMax){
res += leftMax-height[left];
}
else {
res += 0;
leftMax = height[left];
}
}

else {
right--;
if (height[right]<=rightMax){
res += rightMax-height[right];
}
else {
res += 0;
rightMax = height[right];
}
}

}

return res;
}
public static void main(String[] args){
int[] input = {2,1,0,4};
Solution s = new Solution();
System.out.println(s.maxArea(input));
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: