更新 0015.三数之和 排版格式修复

This commit is contained in:
jinbudaily
2023-07-19 14:55:09 +08:00
parent 0922ede8c7
commit eb2cdf24bf

View File

@ -26,14 +26,15 @@
[-1, -1, 2]
]
## 算法公开课
# 思路
针对本题,我录制了视频讲解:[梦破碎的地方!| LeetCode15.三数之和](https://www.bilibili.com/video/BV1GW4y127qo),结合本题解一起看,事半功倍!
**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html)[梦破碎的地方!| LeetCode15.三数之和](https://www.bilibili.com/video/BV1GW4y127qo),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
**注意[0 0 0 0] 这组数据**
## 哈希解法
## 思路
### 哈希解法
两层for循环就可以确定 a 和b 的数值了,可以使用哈希法来确定 0-(a+b) 是否在 数组里出现过,其实这个思路是正确的,但是我们有一个非常棘手的问题,就是题目中说的不可以包含重复的三元组。
@ -87,7 +88,7 @@ public:
* 空间复杂度: O(n),额外的 set 开销
## 双指针
### 双指针
**其实这道题目使用哈希法并不十分合适**因为在去重的操作中有很多细节需要注意在面试中很难直接写出没有bug的代码。
@ -166,9 +167,9 @@ public:
* 空间复杂度: O(1)
## 去重逻辑的思考
### 去重逻辑的思考
### a的去重
#### a的去重
说道去重其实主要考虑三个数的去重 a, b ,c, 对应的就是 nums[i]nums[left]nums[right]
@ -208,7 +209,7 @@ if (i > 0 && nums[i] == nums[i - 1]) {
这是一个非常细节的思考过程。
### b与c的去重
#### b与c的去重
很多同学写本题的时候,去重的逻辑多加了 对right 和left 的去重:(代码中注释部分)
@ -238,7 +239,7 @@ while (right > left) {
所以这种去重 是可以不加的。 仅仅是 把去重的逻辑提前了而已。
# 思考题
## 思考题
既然三数之和可以使用双指针法,我们之前讲过的[1.两数之和](https://programmercarl.com/0001.两数之和.html),可不可以使用双指针法呢?
@ -254,8 +255,8 @@ while (right > left) {
## 其他语言版本
### Java
Java
```Java
class Solution {
public List<List<Integer>> threeSum(int[] nums) {
@ -297,8 +298,9 @@ class Solution {
}
```
Python
### Python
(版本一) 双指针
```Python
class Solution:
def threeSum(self, nums: List[int]) -> List[List[int]]:
@ -366,7 +368,7 @@ class Solution:
return result
```
Go
### Go
```Go
func threeSum(nums []int) [][]int {
@ -407,7 +409,7 @@ func threeSum(nums []int) [][]int {
}
```
javaScript:
### JavaScript:
```js
var threeSum = function(nums) {
@ -512,7 +514,7 @@ var threeSum = function (nums) {
};
```
TypeScript:
### TypeScript:
```typescript
function threeSum(nums: number[]): number[][] {
@ -553,7 +555,8 @@ function threeSum(nums: number[]): number[][] {
};
```
ruby:
### Ruby:
```ruby
def is_valid(strs)
symbol_map = {')' => '(', '}' => '{', ']' => '['}
@ -571,8 +574,8 @@ def is_valid(strs)
end
```
### PHP:
PHP:
```php
class Solution {
/**
@ -613,7 +616,8 @@ class Solution {
}
```
Swift:
### Swift:
```swift
// 双指针法
func threeSum(_ nums: [Int]) -> [[Int]] {
@ -654,7 +658,8 @@ func threeSum(_ nums: [Int]) -> [[Int]] {
}
```
Rust:
### Rust:
```Rust
// 哈希解法
use std::collections::HashSet;
@ -718,7 +723,8 @@ impl Solution {
}
```
C:
### C:
```C
//qsort辅助cmp函数
int cmp(const void* ptr1, const void* ptr2) {
@ -792,7 +798,8 @@ int** threeSum(int* nums, int numsSize, int* returnSize, int** returnColumnSizes
}
```
C#:
### C#:
```csharp
public class Solution
{
@ -850,7 +857,8 @@ public class Solution
}
}
```
Scala:
### Scala:
```scala
object Solution {
// 导包
@ -898,3 +906,4 @@ object Solution {
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>