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

it2025-11-20  5

原题链接

以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--; else beg++; } return res; }

 

转载于:https://www.cnblogs.com/ruoh3kou/p/9933102.html

相关资源:数据结构—成绩单生成器
最新回复(0)