LeetCode题解——42题接雨水题目描述如下:
方法效率:
//双指针
class Solution {
public int trap(int[] height) {
int left_max=0;
int right_max=height.length-1;
int left=0;
int right=height.length-1;
int sum=0;
while(left<right){
if(height[left]<height[right]){
if(height[left]>=height[left_max]){
left_max=left;
}else{
sum=sum+height[left_max]-height[left];
}
left++;
}else{
if(height[right]>=height[right_max]){
right_max=right;
}else{
sum=sum+height[right_max]-height[right];
}
right--;
}
}
return sum;
}
}