算法
盛最多水的容器
https://leetcode.cn/problems/container-with-most-water/
两种方案:
- 暴力暴击
public int maxArea1(int[] height) { int maxValue = 0; for (int i = 0; i < height.length; i++) { for (int length = height.length-1; length > i; length--) { int tempvalue = Math.min(height[i], height[length]) * (length - i); if (tempvalue > maxValue) { maxValue = tempvalue; } } } return maxValue; } - 双指针
class Solution { public int maxArea(int[] height) { int maxValue = 0, i=0, j= height.length-1; while(j>i) { int temValue= Math.min(height[i], height[j]) * (j - i); maxValue = Math.max(maxValue, temValue); if (height[i]< height[j]){ i++; }else { j--; } } return maxValue; } }

浙公网安备 33010602011771号