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 1fef7f4

Browse files
committed
commit solution 15
1 parent a00c281 commit 1fef7f4

File tree

6 files changed

+117
-8
lines changed

6 files changed

+117
-8
lines changed

‎index-tags.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
| --- | --- | --- | --- | --- |
99
| [1](https://leetcode-cn.com/problems/two-sum) | [两数之和](/solution/1-99/0001.two-sum/) | `数组`,`哈希表` | <font color=green>简单</font> ||
1010
| [11](https://leetcode-cn.com/problems/container-with-most-water) | [盛最多水的容器](/solution/1-99/0011.container-with-most-water/) | `数组`,`双指针` | <font color=blue>中等</font> ||
11+
| [15](https://leetcode-cn.com/problems/3sum) | [三数之和](/solution/1-99/0015.3sum/) | `数组`,`双指针` | <font color=blue>中等</font> ||
1112
| [26](https://leetcode-cn.com/problems/remove-duplicates-from-sorted-array) | [删除排序数组中的重复项](/solution/1-99/0026.remove-duplicates-from-sorted-array/) | `数组`,`双指针` | <font color=green>简单</font> ||
1213
| [27](https://leetcode-cn.com/problems/remove-element) | [移除元素](/solution/1-99/0027.remove-element/) | `数组`,`双指针` | <font color=green>简单</font> ||
1314
| [35](https://leetcode-cn.com/problems/search-insert-position) | [搜索插入位置](/solution/1-99/0035.search-insert-position/) | `数组`,`二分查找` | <font color=green>简单</font> ||
@@ -67,6 +68,7 @@
6768
| 题号 | 题解 | 标签 | 难度 | 是否解题 |
6869
| --- | --- | --- | --- | --- |
6970
| [11](https://leetcode-cn.com/problems/container-with-most-water) | [盛最多水的容器](/solution/1-99/0011.container-with-most-water/) | `数组`,`双指针` | <font color=blue>中等</font> ||
71+
| [15](https://leetcode-cn.com/problems/3sum) | [三数之和](/solution/1-99/0015.3sum/) | `数组`,`双指针` | <font color=blue>中等</font> ||
7072
| [26](https://leetcode-cn.com/problems/remove-duplicates-from-sorted-array) | [删除排序数组中的重复项](/solution/1-99/0026.remove-duplicates-from-sorted-array/) | `数组`,`双指针` | <font color=green>简单</font> ||
7173
| [27](https://leetcode-cn.com/problems/remove-element) | [移除元素](/solution/1-99/0027.remove-element/) | `数组`,`双指针` | <font color=green>简单</font> ||
7274
| [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
@@ -412,7 +412,7 @@
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> |
413413
| [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> |
415-
| [15](https://leetcode-cn.com/problems/3sum) | [三数之和](/solution/1-99/0015.3sum/) | `数组`,`双指针` | <font color=blue>中等</font> |
415+
| [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> |
417417
| [17](https://leetcode-cn.com/problems/letter-combinations-of-a-phone-number) | [电话号码的字母组合](/solution/1-99/0017.letter-combinations-of-a-phone-number/) | `字符串`,`回溯算法` | <font color=blue>中等</font> |
418418
| [18](https://leetcode-cn.com/problems/4sum) | [四数之和](/solution/1-99/0018.4sum/) | `数组`,`哈希表`,`双指针` | <font color=blue>中等</font> |

‎solution/1-99/0015.3sum/README.md‎

Lines changed: 47 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,61 @@
1-
# [15. xxx](https://leetcode-cn.com/problems/longest-common-prefix)
1+
# [15. 三数之和](https://leetcode-cn.com/problems/3sum/description/)
22

33
### 题目描述
44

5+
<p>给你一个包含 <em>n</em> 个整数的数组&nbsp;<code>nums</code>,判断&nbsp;<code>nums</code>&nbsp;中是否存在三个元素 <em>a,b,c ,</em>使得&nbsp;<em>a + b + c = </em>0 ?请你找出所有满足条件且不重复的三元组。</p>
56

7+
<p><strong>注意:</strong>答案中不可以包含重复的三元组。</p>
8+
9+
<p>&nbsp;</p>
10+
11+
<p><strong>示例:</strong></p>
12+
13+
<pre>给定数组 nums = [-1, 0, 1, 2, -1, -4],
14+
15+
满足要求的三元组集合为:
16+
[
17+
[-1, 0, 1],
18+
[-1, -1, 2]
19+
]
20+
</pre>
621

722
### 解题思路
823

24+
![](http://lc-photo.xwlin.com/15.gif)
925

1026
### 具体解法
1127

12-
<!-- tabs:start -->
13-
1428
#### **Golang**
1529
```go
16-
30+
func threeSum(nums []int) [][]int {
31+
sort.Ints(nums)
32+
flagMap := make(map[string]bool)
33+
var res [][]int
34+
i := 0
35+
for i < len(nums) {
36+
if i-1 >= 0 && nums[i] == nums[i-1] {
37+
i++
38+
continue
39+
}
40+
p, q := 1, len(nums)-1
41+
for p+i < q {
42+
if nums[i]+nums[p+i]+nums[q] == 0 {
43+
flag := strconv.Itoa(nums[i]) + strconv.Itoa(nums[p+i]) + strconv.Itoa(nums[q])
44+
if _, ok := flagMap[flag]; !ok {
45+
res = append(res, []int{nums[i], nums[p+i], nums[q]})
46+
}
47+
flagMap[flag] = true
48+
p++
49+
q--
50+
} else if nums[i]+nums[p+i]+nums[q] > 0 {
51+
q--
52+
} else if nums[i]+nums[p+i]+nums[q] < 9 {
53+
p++
54+
}
55+
}
56+
i++
57+
}
58+
return res
59+
}
1760
```
1861

19-
<!-- tabs:end -->
20-

‎solution/1-99/0015.3sum/solution.go‎

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package leetcode
2+
3+
import (
4+
"sort"
5+
"strconv"
6+
)
7+
8+
/*
9+
* @lc app=leetcode.cn id=15 lang=golang
10+
*
11+
* [15] 三数之和
12+
*/
13+
14+
// @lc code=start
15+
func threeSum(nums []int) [][]int {
16+
sort.Ints(nums)
17+
flagMap := make(map[string]bool)
18+
var res [][]int
19+
i := 0
20+
for i < len(nums) {
21+
if i-1 >= 0 && nums[i] == nums[i-1] {
22+
i++
23+
continue
24+
}
25+
p, q := 1, len(nums)-1
26+
for p+i < q {
27+
if nums[i]+nums[p+i]+nums[q] == 0 {
28+
flag := strconv.Itoa(nums[i]) + strconv.Itoa(nums[p+i]) + strconv.Itoa(nums[q])
29+
if _, ok := flagMap[flag]; !ok {
30+
res = append(res, []int{nums[i], nums[p+i], nums[q]})
31+
}
32+
flagMap[flag] = true
33+
p++
34+
q--
35+
} else if nums[i]+nums[p+i]+nums[q] > 0 {
36+
q--
37+
} else if nums[i]+nums[p+i]+nums[q] < 9 {
38+
p++
39+
}
40+
}
41+
i++
42+
}
43+
return res
44+
}
45+
46+
// @lc code=end
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package leetcode
2+
3+
import (
4+
"testing"
5+
)
6+
7+
func TestThreeSum(t *testing.T) {
8+
var ret [][]int
9+
var nums []int
10+
11+
nums = []int{-1, 0, 1, 2, -1, -4}
12+
ret = [][]int{{-1, -1, 2}, {-1, 0, 1}}
13+
for k, num := range threeSum(nums) {
14+
for i, v := range num {
15+
if v != ret[k][i] {
16+
t.Fatalf("case fails %v\n", ret)
17+
}
18+
}
19+
}
20+
}

‎solution/1-99/_sidebar.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
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/)
23-
- [15. 三数之和](solution/1-99/0015.3sum/)
23+
- [15. 三数之和](solution/1-99/0015.3sum/)
2424
- [16. 最接近的三数之和](solution/1-99/0016.3sum-closest/)
2525
- [17. 电话号码的字母组合](solution/1-99/0017.letter-combinations-of-a-phone-number/)
2626
- [18. 四数之和](solution/1-99/0018.4sum/)

0 commit comments

Comments
(0)

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