11. 盛最多水的容器

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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
//双指针法
class Solution {
public int maxArea(int[] height) {
int ans = 0;
int area = 0;
int left = 0;
int right = height.length-1;
while (left < right) {
area = Math.min(height[left], height[right])*(right-left);
ans = Math.max(area, ans);
if(height[left] >= height[right]) {
right--;
}else{
left++;
}
}
return ans;
}
}

//暴力法
class Violence {
public int maxArea(int[] height) {
int max = -1;
int area = 0;
int left = 0;
int right = 0;
int wide = height.length;
int blow = wide -1;
for(left=0; left < wide; left++) {
blow = wide- 1 - left;
for(right = wide -1; right> left ; right --) {
if(height[left] <= height[right]) {
area = height[left]*blow;
blow --;
}else{
area = height[right]*blow;
blow --;
}
if(area >= max) {
max = area;
}
}
}
return max;
}
}

11. 盛最多水的容器
https://pisces34.github.io/2021/05/08/leetcode/ContainerWithMostWater/
发布于
2021年5月8日
许可协议