42. 接雨水
https://leetcode-cn.com/problems/trapping-rain-water/
双指针解法。
1 public class Solution 2 { 3 public int Trap(int[] height) 4 { 5 int size = height.Length, left = 0, right = size - 1, ans = 0, leftMax = 0, rightMax = 0; 6 while (left < right) 7 { 8 if (height[left] < height[right]) 9 { 10 if (height[left] >= leftMax) 11 { 12 leftMax = height[left]; 13 } 14 else 15 { 16 ans += (leftMax - height[left]); 17 } 18 left++; 19 } 20 else 21 { 22 if (height[right] >= rightMax) 23 { 24 rightMax = height[right]; 25 } 26 else 27 { 28 ans += (rightMax - height[right]); 29 } 30 right--; 31 } 32 } 33 return ans; 34 } 35 }