Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit a00c281

Browse files
committed
commit solution 11
1 parent 19b4c74 commit a00c281

File tree

6 files changed

+68
-6
lines changed

6 files changed

+68
-6
lines changed

‎index-tags.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
| 题号 | 题解 | 标签 | 难度 | 是否解题 |
88
| --- | --- | --- | --- | --- |
99
| [1](https://leetcode-cn.com/problems/two-sum) | [两数之和](/solution/1-99/0001.two-sum/) | `数组`,`哈希表` | <font color=green>简单</font> ||
10+
| [11](https://leetcode-cn.com/problems/container-with-most-water) | [盛最多水的容器](/solution/1-99/0011.container-with-most-water/) | `数组`,`双指针` | <font color=blue>中等</font> ||
1011
| [26](https://leetcode-cn.com/problems/remove-duplicates-from-sorted-array) | [删除排序数组中的重复项](/solution/1-99/0026.remove-duplicates-from-sorted-array/) | `数组`,`双指针` | <font color=green>简单</font> ||
1112
| [27](https://leetcode-cn.com/problems/remove-element) | [移除元素](/solution/1-99/0027.remove-element/) | `数组`,`双指针` | <font color=green>简单</font> ||
1213
| [35](https://leetcode-cn.com/problems/search-insert-position) | [搜索插入位置](/solution/1-99/0035.search-insert-position/) | `数组`,`二分查找` | <font color=green>简单</font> ||
@@ -65,6 +66,7 @@
6566

6667
| 题号 | 题解 | 标签 | 难度 | 是否解题 |
6768
| --- | --- | --- | --- | --- |
69+
| [11](https://leetcode-cn.com/problems/container-with-most-water) | [盛最多水的容器](/solution/1-99/0011.container-with-most-water/) | `数组`,`双指针` | <font color=blue>中等</font> ||
6870
| [26](https://leetcode-cn.com/problems/remove-duplicates-from-sorted-array) | [删除排序数组中的重复项](/solution/1-99/0026.remove-duplicates-from-sorted-array/) | `数组`,`双指针` | <font color=green>简单</font> ||
6971
| [27](https://leetcode-cn.com/problems/remove-element) | [移除元素](/solution/1-99/0027.remove-element/) | `数组`,`双指针` | <font color=green>简单</font> ||
7072
| [28](https://leetcode-cn.com/problems/implement-strstr) | [实现 strstr()](/solution/1-99/0028.implement-strstr%28%29/) | `双指针`,`字符串` | <font color=green>简单</font> ||

‎index-type.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -410,7 +410,7 @@
410410
| [5](https://leetcode-cn.com/problems/longest-palindromic-substring) | [最长回文子串](/solution/1-99/0005.longest-palindromic-substring/) | `字符串`,`动态规划` | <font color=blue>中等</font> |
411411
| [6](https://leetcode-cn.com/problems/zigzag-conversion) | [z 字形变换](/solution/1-99/0006.zigzag-conversion/) | `字符串` | <font color=blue>中等</font> |
412412
| [8](https://leetcode-cn.com/problems/string-to-integer-atoi) | [字符串转换整数 (atoi)](/solution/1-99/0008.string-to-integer-%28atoi%29/) | `数学`,`字符串` | <font color=blue>中等</font> |
413-
| [11](https://leetcode-cn.com/problems/container-with-most-water) | [盛最多水的容器](/solution/1-99/0011.container-with-most-water/) | `数组`,`双指针` | <font color=blue>中等</font> |
413+
| [11](https://leetcode-cn.com/problems/container-with-most-water) | [盛最多水的容器](/solution/1-99/0011.container-with-most-water/) | `数组`,`双指针` | <font color=blue>中等</font> ||
414414
| [12](https://leetcode-cn.com/problems/integer-to-roman) | [整数转罗马数字](/solution/1-99/0012.integer-to-roman/) | `数学`,`字符串` | <font color=blue>中等</font> |
415415
| [15](https://leetcode-cn.com/problems/3sum) | [三数之和](/solution/1-99/0015.3sum/) | `数组`,`双指针` | <font color=blue>中等</font> |
416416
| [16](https://leetcode-cn.com/problems/3sum-closest) | [最接近的三数之和](/solution/1-99/0016.3sum-closest/) | `数组`,`双指针` | <font color=blue>中等</font> |

‎solution/1-99/0011.container-with-most-water/README.md‎

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
### 题目描述
44

5-
<!-- 这里写题目描述 -->
65
<p>给你 <em>n</em> 个非负整数 <em>a</em><sub>1</sub>,<em>a</em><sub>2,</sub>...,<em>a</em><sub>n,</sub>每个数代表坐标中的一个点&nbsp;(<em>i</em>,&nbsp;<em>a<sub>i</sub></em>) 。在坐标内画 <em>n</em> 条垂直线,垂直线 <em>i</em>&nbsp;的两个端点分别为&nbsp;(<em>i</em>,&nbsp;<em>a<sub>i</sub></em>) 和 (<em>i</em>, 0)。找出其中的两条线,使得它们与&nbsp;<em>x</em>&nbsp;轴共同构成的容器可以容纳最多的水。</p>
76

87
<p><strong>说明:</strong>你不能倾斜容器,且&nbsp;<em>n</em>&nbsp;的值至少为 2。</p>
@@ -24,14 +23,30 @@
2423

2524
### 解题思路
2625

26+
1. 双指针
2727

2828
### 具体解法
2929

30-
<!-- tabs:start -->
3130

3231
#### **Golang**
3332
```go
34-
33+
func maxArea(height []int) int {
34+
p, q := 0, len(height)-1
35+
var ret int
36+
var tmp int
37+
for p < q {
38+
if height[p] < height[q] {
39+
tmp = height[p] * (q - p)
40+
p++
41+
} else {
42+
tmp = height[q] * (q - p)
43+
q--
44+
}
45+
if ret < tmp {
46+
ret = tmp
47+
}
48+
}
49+
return ret
50+
}
3551
```
3652

37-
<!-- tabs:end -->
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package leetcode
2+
3+
/*
4+
* @lc app=leetcode.cn id=11 lang=golang
5+
*
6+
* [11] 盛最多水的容器
7+
*/
8+
9+
// @lc code=start
10+
func maxArea(height []int) int {
11+
p, q := 0, len(height)-1
12+
var ret int
13+
var tmp int
14+
for p < q {
15+
if height[p] < height[q] {
16+
tmp = height[p] * (q - p)
17+
p++
18+
} else {
19+
tmp = height[q] * (q - p)
20+
q--
21+
}
22+
if ret < tmp {
23+
ret = tmp
24+
}
25+
}
26+
return ret
27+
}
28+
29+
// @lc code=end
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package leetcode
2+
3+
import (
4+
"testing"
5+
)
6+
7+
func TestMaxArea(t *testing.T) {
8+
var ret int
9+
var nums []int
10+
11+
nums = []int{1, 8, 6, 2, 5, 4, 8, 3, 7}
12+
ret = 49
13+
if ret != maxArea(nums) {
14+
t.Fatalf("case fails %v\n", ret)
15+
}
16+
}

‎solution/1-99/_sidebar.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
- [8. 字符串转换整数 (atoi)](solution/1-99/0008.string-to-integer-atoi/)
1717
- [9. 回文数 ✅](solution/1-99/0009.palindrome-number/)
1818
- [10. 正则表达式匹配](solution/1-99/0010.regular-expression-matching/)
19-
- [11. 盛最多水的容器](solution/1-99/0011.container-with-most-water/)
19+
- [11. 盛最多水的容器](solution/1-99/0011.container-with-most-water/)
2020
- [12. 整数转罗马数字](solution/1-99/0012.integer-to-roman/)
2121
- [13. 罗马数字转整数 ✅](solution/1-99/0013.roman-to-integer/)
2222
- [14. 最长公共前缀](solution/1-99/0014.longest-common-prefix/)

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /