File tree Expand file tree Collapse file tree 1 file changed +35
-0
lines changed
src/main/java/com/geekidentity/leetcode/n0011 Expand file tree Collapse file tree 1 file changed +35
-0
lines changed Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments