Skip to content

Commit 2394638

Browse files
Update
1 parent cb69d59 commit 2394638

File tree

1 file changed

+5
-13
lines changed

1 file changed

+5
-13
lines changed

problems/0015.三数之和.md

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -24,25 +24,27 @@ public:
2424
vector<vector<int>> threeSum(vector<int>& nums) {
2525
vector<vector<int>> result;
2626
sort(nums.begin(), nums.end());
27+
// 找出a + b + c = 0
28+
// a = nums[i], b = nums[j], c = -(a + b)
2729
for (int i = 0; i < nums.size(); i++) {
2830
// 排序之后如果第一个元素已经大于零,那么不可能凑成三元组
2931
if (nums[i] > 0) {
3032
continue;
3133
}
32-
if (i > 0 && nums[i] == nums[i - 1]) { //三元组第一个元素去重
34+
if (i > 0 && nums[i] == nums[i - 1]) { //三元组元素a去重
3335
continue;
3436
}
3537
unordered_set<int> set;
3638
for (int j = i + 1; j < nums.size(); j++) {
3739
if (j > i + 2
3840
&& nums[j] == nums[j-1]
39-
&& nums[j-1] == nums[j-2]) { // 三元组第三个元素去重
41+
&& nums[j-1] == nums[j-2]) { // 三元组元素b去重
4042
continue;
4143
}
4244
int c = 0 - (nums[i] + nums[j]);
4345
if (set.find(c) != set.end()) {
4446
result.push_back({nums[i], nums[j], c});
45-
set.erase(c);// 三元组第二个元素去重
47+
set.erase(c);// 三元组元素c去重
4648
} else {
4749
set.insert(nums[j]);
4850
}
@@ -52,16 +54,6 @@ public:
5254
}
5355
};
5456
```
55-
56-
```
57-
Input
58-
[-2,0,0,2,2]
59-
Output
60-
[[-2,2,0],[-2,2,0]]
61-
Expected
62-
[[-2,0,2]]
63-
```
64-
6557
## 双指针法代码
6658
```
6759
class Solution {

0 commit comments

Comments
 (0)