Skip to content

Commit f4c4a75

Browse files
committed
11. 盛最多水的容器
1 parent 7854087 commit f4c4a75

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.geekidentity.leetcode.n0011;
2+
3+
/**
4+
* 11. 盛最多水的容器
5+
* https://leetcode-cn.com/problems/container-with-most-water/
6+
*/
7+
public class MaxArea {
8+
9+
/**
10+
* 暴力方法
11+
*/
12+
public int maxArea1(int[] height) {
13+
int maxArea = 0;
14+
for (int i = 0; i < height.length - 1; i++) {
15+
for (int j = i + 1; j < height.length; j++) {
16+
int area = Math.min(height[i], height[j]) * (j - i);
17+
if (area > maxArea) maxArea = area;
18+
}
19+
}
20+
return maxArea;
21+
}
22+
23+
/**
24+
* 双指针法
25+
*/
26+
public int maxArea2(int[] height) {
27+
int maxArea = 0;
28+
for (int i = 0, j = height.length - 1; i < j;) {
29+
int minHeight = height[i] < height[j] ? height[i++] : height[j++];
30+
int area = minHeight * (j - i + 1);
31+
if (area > maxArea) maxArea = area;
32+
}
33+
return maxArea;
34+
}
35+
}

0 commit comments

Comments
 (0)