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)); } }
相关文章推荐
- Java for LeetCode 042 Trapping Rain Water
- LeetCode Trapping Rain Water
- Leetcode 407 Trapping Rain Water II
- leetcode之Trapping Rain Water
- LeetCode—Trapping Rain Water 储水问题,从两边向中间
- [LeetCode]题解(python):042-Trapping Rain Water
- [LeetCode]42 Trapping Rain Water
- leetcode数组之Trapping Rain Water
- LeetCode Trapping Rain Water等雨水的凹槽容量
- LeetCode Trapping Rain Water
- 【leetcode】Trapping Rain Water
- leetcode || 42、 Trapping Rain Water
- [LeetCode] Trapping rain water
- leetcode -- Trapping Rain Water -- 重点
- [LeetCode] Trapping Rain Water
- Trapping Rain Water - Leetcode
- leetcode: Trapping Rain Water
- Leetcode Trapping Rain Water
- Leetcode—— trapping-rain-water
- leetcode 42:Trapping Rain Water