Junedayday Blog

六月天天的个人博客

0%

Go算法实战 - 7.【盛最多水的容器LeetCode-11】

Go-Leetcode

Leetcode-11 盛最多水的容器

原题链接 https://leetcode-cn.com/problems/container-with-most-water/

1
2
func maxArea(height []int) int {
}

题解

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
func maxArea(height []int) int {
// 左右两个指针往中间逼近
left, right := 0, len(height)-1
var max int
for left < right {
var area int
// 哪边高度低,就挪哪边
if height[left] < height[right] {
area = (right - left) * height[left]
left++
} else {
area = (right - left) * height[right]
right--
}
if area > max {
max = area
}
}

return max
}

Github: https://github.com/Junedayday/code_reading

Blog: http://junes.tech/

Bilibili: https://space.bilibili.com/293775192

公众号: golangcoding

二维码