Table of Content
这道题可以采用双指针的方法,每次移动有可能会增大面积的那条边,具体的思维方式可以看网站的解答,我这里给出Python实现代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
<br />class Solution: def maxArea(self, height): """ :type height: List[int] :rtype: int """ height_list = height len_height = len(height_list) if len_height < 2: return 0 max_area = 0 i = 0 j = len_height - 1 while i != j: h = min(height_list[i], height_list[j]) w = j - i if max_area < h * w: max_area = h * w if height_list[i] < height_list[j]: i = i + 1 else: j = j - 1 return max_area |
如果您有更好的思路,欢迎讨论。