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

[pull] master from youngyangyang04:master #307

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
pull merged 20 commits into AlgorithmAndLeetCode:master from youngyangyang04:master
Jul 20, 2023
Merged
Changes from 1 commit
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
607e4eb
更新 哈希表理论基础 排版格式修复
jinbudaily Jul 19, 2023
c628aa6
更新 0242.有效的字母异位词 排版格式修复
jinbudaily Jul 19, 2023
19dfa98
更新 0349.两个数组的交集 排版格式修复
jinbudaily Jul 19, 2023
764b3e9
更新 0202.快乐数 排版格式修复
jinbudaily Jul 19, 2023
23950d1
更新 001.两数之和 排版格式修复
jinbudaily Jul 19, 2023
2948791
更新 0454.四数相加II 排版格式修复
jinbudaily Jul 19, 2023
0922ede
更新 0383.赎金信 排版格式修复
jinbudaily Jul 19, 2023
eb2cdf2
更新 0015.三数之和 排版格式修复
jinbudaily Jul 19, 2023
3fe673d
更新 0018.四数之和 排版格式修复
jinbudaily Jul 19, 2023
ddee6ad
更新 哈希表总结 排版格式修复
jinbudaily Jul 19, 2023
4ae8843
Merge branch 'master' of github.com:jinbudaily/leetcode-master
jinbudaily Jul 19, 2023
00ef608
更新 0344.反转字符串 排版格式修复
jinbudaily Jul 19, 2023
5cb2501
更新 0541.反转字符串 排版格式修复
jinbudaily Jul 19, 2023
963eb8f
更新 剑指Offer05.替换空格 排版格式修复
jinbudaily Jul 19, 2023
e70ca92
更新 0151.反转字符串中的单词 排版格式修复
jinbudaily Jul 19, 2023
370a4d1
更新 剑指Offer58-II.左旋转字符串 排版格式修复
jinbudaily Jul 19, 2023
5acebcc
更新 0028.实现strStr 排版格式修复
jinbudaily Jul 19, 2023
202dd38
更新 0459.重复的字符串 排版格式修复
jinbudaily Jul 19, 2023
4fe1f08
更新 字符串总结 排版格式修复
jinbudaily Jul 19, 2023
406cada
Merge pull request #2191 from jinbudaily/master
youngyangyang04 Jul 20, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
更新 0015.三数之和 排版格式修复
  • Loading branch information
jinbudaily committed Jul 19, 2023
commit eb2cdf24bf03dd143072a4260fb11b76ececcc37
55 changes: 32 additions & 23 deletions problems/0015.三数之和.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,15 @@
[-1, -1, 2]
]

## 算法公开课

# 思路

针对本题,我录制了视频讲解:[梦破碎的地方!| LeetCode:15.三数之和](https://www.bilibili.com/video/BV1GW4y127qo),结合本题解一起看,事半功倍!
**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[梦破碎的地方!| LeetCode:15.三数之和](https://www.bilibili.com/video/BV1GW4y127qo),相信结合视频再看本篇题解,更有助于大家对本题的理解**。

**注意[0, 0, 0, 0] 这组数据**

## 哈希解法
## 思路

### 哈希解法

两层for循环就可以确定 a 和b 的数值了,可以使用哈希法来确定 0-(a+b) 是否在 数组里出现过,其实这个思路是正确的,但是我们有一个非常棘手的问题,就是题目中说的不可以包含重复的三元组。

Expand Down Expand Up @@ -87,7 +88,7 @@ public:
* 空间复杂度: O(n),额外的 set 开销


## 双指针
### 双指针

**其实这道题目使用哈希法并不十分合适**,因为在去重的操作中有很多细节需要注意,在面试中很难直接写出没有bug的代码。

Expand Down Expand Up @@ -166,9 +167,9 @@ public:
* 空间复杂度: O(1)


## 去重逻辑的思考
### 去重逻辑的思考

### a的去重
#### a的去重

说道去重,其实主要考虑三个数的去重。 a, b ,c, 对应的就是 nums[i],nums[left],nums[right]

Expand All @@ -188,7 +189,7 @@ a 如果重复了怎么办,a是nums里遍历的元素,那么应该直接跳
if (nums[i] == nums[i + 1]) { // 去重操作
continue;
}
```
```

那就我们就把 三元组中出现重复元素的情况直接pass掉了。 例如{-1, -1 ,2} 这组数据,当遍历到第一个-1 的时候,判断 下一个也是-1,那这组数据就pass了。

Expand All @@ -208,7 +209,7 @@ if (i > 0 && nums[i] == nums[i - 1]) {

这是一个非常细节的思考过程。

### b与c的去重
#### b与c的去重

很多同学写本题的时候,去重的逻辑多加了 对right 和left 的去重:(代码中注释部分)

Expand All @@ -225,7 +226,7 @@ while (right > left) {
} else {
}
}
```
```

但细想一下,这种去重其实对提升程序运行效率是没有帮助的。

Expand All @@ -238,7 +239,7 @@ while (right > left) {
所以这种去重 是可以不加的。 仅仅是 把去重的逻辑提前了而已。


# 思考题
## 思考题


既然三数之和可以使用双指针法,我们之前讲过的[1.两数之和](https://programmercarl.com/0001.两数之和.html),可不可以使用双指针法呢?
Expand All @@ -254,8 +255,8 @@ while (right > left) {

## 其他语言版本

### Java:

Java:
```Java
class Solution {
public List<List<Integer>> threeSum(int[] nums) {
Expand Down Expand Up @@ -297,8 +298,9 @@ class Solution {
}
```

Python:
### Python:
(版本一) 双指针

```Python
class Solution:
def threeSum(self, nums: List[int]) -> List[List[int]]:
Expand Down Expand Up @@ -366,7 +368,7 @@ class Solution:
return result
```

Go:
### Go:

```Go
func threeSum(nums []int) [][]int {
Expand Down Expand Up @@ -407,7 +409,7 @@ func threeSum(nums []int) [][]int {
}
```

javaScript:
### JavaScript:

```js
var threeSum = function(nums) {
Expand Down Expand Up @@ -512,7 +514,7 @@ var threeSum = function (nums) {
};
```

TypeScript:
### TypeScript:

```typescript
function threeSum(nums: number[]): number[][] {
Expand Down Expand Up @@ -553,7 +555,8 @@ function threeSum(nums: number[]): number[][] {
};
```

ruby:
### Ruby:

```ruby
def is_valid(strs)
symbol_map = {')' => '(', '}' => '{', ']' => '['}
Expand All @@ -571,8 +574,8 @@ def is_valid(strs)
end
```

### PHP:

PHP:
```php
class Solution {
/**
Expand Down Expand Up @@ -613,7 +616,8 @@ class Solution {
}
```

Swift:
### Swift:

```swift
// 双指针法
func threeSum(_ nums: [Int]) -> [[Int]] {
Expand Down Expand Up @@ -654,7 +658,8 @@ func threeSum(_ nums: [Int]) -> [[Int]] {
}
```

Rust:
### Rust:

```Rust
// 哈希解法
use std::collections::HashSet;
Expand Down Expand Up @@ -718,7 +723,8 @@ impl Solution {
}
```

C:
### C:

```C
//qsort辅助cmp函数
int cmp(const void* ptr1, const void* ptr2) {
Expand Down Expand Up @@ -792,7 +798,8 @@ int** threeSum(int* nums, int numsSize, int* returnSize, int** returnColumnSizes
}
```

C#:
### C#:

```csharp
public class Solution
{
Expand Down Expand Up @@ -850,7 +857,8 @@ public class Solution
}
}
```
Scala:
### Scala:

```scala
object Solution {
// 导包
Expand Down Expand Up @@ -898,3 +906,4 @@ object Solution {
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>

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