更新 0018.四数之和 排版格式修复

This commit is contained in:
jinbudaily
2023-07-19 14:59:05 +08:00
parent eb2cdf24bf
commit 3fe673d804

View File

@ -27,9 +27,11 @@
[-2, 0, 0, 2] [-2, 0, 0, 2]
] ]
# 思路 ## 算法公开课
针对本题,我录制了视频讲解[难在去重和剪枝!| LeetCode18. 四数之和](https://www.bilibili.com/video/BV1DS4y147US)结合本题解一起看,事半功倍! **[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html)[难在去重和剪枝!| LeetCode18. 四数之和](https://www.bilibili.com/video/BV1DS4y147US)相信结合视频再看本篇题解,更有助于大家对本题的理解**。
## 思路
四数之和,和[15.三数之和](https://programmercarl.com/0015.三数之和.html)是一个思路,都是使用双指针法, 基本解法就是在[15.三数之和](https://programmercarl.com/0015.三数之和.html) 的基础上再套一层for循环。 四数之和,和[15.三数之和](https://programmercarl.com/0015.三数之和.html)是一个思路,都是使用双指针法, 基本解法就是在[15.三数之和](https://programmercarl.com/0015.三数之和.html) 的基础上再套一层for循环。
@ -141,22 +143,16 @@ if (nums[k] + nums[i] > target && nums[k] + nums[i] >= 0) {
if (nums[k] + nums[i] > target && nums[i] >= 0) { if (nums[k] + nums[i] > target && nums[i] >= 0) {
break; break;
} }
``` ```
因为只要 nums[k] + nums[i] > target那么 nums[i] 后面的数都是正数的话,就一定 不符合条件了。 因为只要 nums[k] + nums[i] > target那么 nums[i] 后面的数都是正数的话,就一定 不符合条件了。
不过这种剪枝 其实有点 小绕,大家能够理解 文章给的完整代码的剪枝 就够了。 不过这种剪枝 其实有点 小绕,大家能够理解 文章给的完整代码的剪枝 就够了。
## 其他语言版本 ## 其他语言版本
### Java
Java
```Java ```Java
class Solution { class Solution {
public List<List<Integer>> fourSum(int[] nums, int target) { public List<List<Integer>> fourSum(int[] nums, int target) {
@ -206,8 +202,9 @@ class Solution {
} }
``` ```
Python ### Python
(版本一) 双指针 (版本一) 双指针
```python ```python
class Solution: class Solution:
def fourSum(self, nums: List[int], target: int) -> List[List[int]]: def fourSum(self, nums: List[int], target: int) -> List[List[int]]:
@ -273,7 +270,8 @@ class Solution(object):
``` ```
Go ### Go
```go ```go
func fourSum(nums []int, target int) [][]int { func fourSum(nums []int, target int) [][]int {
if len(nums) < 4 { if len(nums) < 4 {
@ -323,7 +321,7 @@ func fourSum(nums []int, target int) [][]int {
} }
``` ```
javaScript: ### JavaScript:
```js ```js
/** /**
@ -359,7 +357,7 @@ var fourSum = function(nums, target) {
}; };
``` ```
TypeScript ### TypeScript
```typescript ```typescript
function fourSum(nums: number[], target: number): number[][] { function fourSum(nums: number[], target: number): number[][] {
@ -400,7 +398,7 @@ function fourSum(nums: number[], target: number): number[][] {
}; };
``` ```
PHP: ### PHP:
```php ```php
class Solution { class Solution {
@ -445,7 +443,8 @@ class Solution {
} }
``` ```
Swift: ### Swift:
```swift ```swift
func fourSum(_ nums: [Int], _ target: Int) -> [[Int]] { func fourSum(_ nums: [Int], _ target: Int) -> [[Int]] {
var res = [[Int]]() var res = [[Int]]()
@ -493,7 +492,8 @@ func fourSum(_ nums: [Int], _ target: Int) -> [[Int]] {
} }
``` ```
C#: ### C#:
```csharp ```csharp
public class Solution public class Solution
{ {
@ -555,7 +555,8 @@ public class Solution
} }
``` ```
Rust: ### Rust:
```Rust ```Rust
use std::cmp::Ordering; use std::cmp::Ordering;
impl Solution { impl Solution {
@ -603,7 +604,8 @@ impl Solution {
} }
``` ```
Scala: ### Scala:
```scala ```scala
object Solution { object Solution {
// 导包 // 导包
@ -651,3 +653,4 @@ object Solution {
<a href="https://programmercarl.com/other/kstar.html" target="_blank"> <a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/> <img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a> </a>