```
public class Solution {
public int trap(int[] A) {
int len = A.length;
if(len < 3)return 0;
int sum = 0;
int left = 0;
int lheight = A[left];
int right = len - 1;
int rheight = A[right];
while(left < right){
while(left < right && lheight <= rheight){
left++;
if(A[left] < lheight){
sum += lheight - A[left];
}else
lheight = A[left];
}
while(left < right && lheight > rheight){
right--;
if(A[right] < rheight){
sum += rheight - A[right];
}else
rheight = A[right];
}
}
return sum;
}
```

}