记录的最大高度从前进方向向后的方向,叫他们leftmax和rightmax。
public int trap(int[] height) {
int left = 0;
int right = height.length - 1;
int max = 0;
int leftmax = 0;
int rightmax = 0;
while(left <= right) {
leftmax = Math.max(leftmax,height[left]);
rightmax = Math.max(rightmax,height[right]);
if(leftmax < rightmax) {
max += (leftmax - height[left]);
left++;
} else {
max += (rightmax - height[right]);
right--;
}
}
return max;
}