当前位置: 代码迷 >> 综合 >> [leetcode] 11. Container With Most Water (medium)
  详细解决方案

[leetcode] 11. Container With Most Water (medium)

热度:13   发布时间:2024-01-05 01:06:40.0

原题链接

以Y坐标长度作为木桶边界,以X坐标差为桶底,找出可装多少水。

思路:

前后遍历。

Runtime: 5 ms, faster than 95.28% of Java

class Solution {public int maxArea(int[] height) {int res = 0;int beg = 0;int end = height.length - 1;int temp = 0;while (beg != end) {temp = Math.min(height[beg], height[end]) * (end - beg);res = temp > res ? temp : res;if (height[beg] > height[end])end--;elsebeg++;}return res;}

 

  相关解决方案