Skip to content

Commit 4037eb6

Browse files
Merge branch 'master' of github.com:youngyangyang04/leetcode-master
2 parents 9693ad4 + 2691e27 commit 4037eb6

File tree

153 files changed

+2209
-1653
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

153 files changed

+2209
-1653
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -386,7 +386,7 @@
386386

387387
* [图论:深度优先搜索理论基础](./problems/图论深搜理论基础.md)
388388
* [图论:797.所有可能的路径](./problems/0797.所有可能的路径.md)
389-
* [图论:广度优先搜索理论基础](./problems/图论广索理论基础.md)
389+
* [图论:广度优先搜索理论基础](./problems/图论广搜理论基础.md)
390390
* [图论:200.岛屿数量.深搜版](./problems/0200.岛屿数量.深搜版.md)
391391
* [图论:200.岛屿数量.广搜版](./problems/0200.岛屿数量.广搜版.md)
392392
* [图论:695.岛屿的最大面积](./problems/0695.岛屿的最大面积.md)

problems/0001.两数之和.md

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<p align="center"><strong><a href="https://mp.weixin.qq.com/s/tqCxrMEU-ajQumL1i8im9A">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!</strong></p>
66

77

8-
## 1. 两数之和
8+
# 1. 两数之和
99

1010
[力扣题目链接](https://leetcode.cn/problems/two-sum/)
1111

@@ -21,10 +21,12 @@
2121

2222
所以返回 [0, 1]
2323

24+
## 算法公开课
25+
26+
**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html)[梦开始的地方,Leetcode:1.两数之和](https://www.bilibili.com/video/BV1aT41177mK),相信结合视频再看本篇题解,更有助于大家对本题的理解**
2427

25-
## 思路
2628

27-
建议看一下我录的这期视频:[梦开始的地方,Leetcode:1.两数之和](https://www.bilibili.com/video/BV1aT41177mK),结合本题解来学习,事半功倍。
29+
## 思路
2830

2931
很明显暴力的解法是两层for循环查找,时间复杂度是O(n^2)。
3032

@@ -128,8 +130,8 @@ public:
128130
129131
## 其他语言版本
130132
133+
### Java:
131134
132-
Java:
133135
```java
134136
public int[] twoSum(int[] nums, int target) {
135137
int[] res = new int[2];
@@ -150,8 +152,9 @@ public int[] twoSum(int[] nums, int target) {
150152
}
151153
```
152154

153-
Python:
155+
### Python:
154156
(版本一) 使用字典
157+
155158
```python
156159
class Solution:
157160
def twoSum(self, nums: List[int], target: int) -> List[int]:
@@ -160,7 +163,7 @@ class Solution:
160163
for index, value in enumerate(nums):
161164
if target - value in records: # 遍历当前元素,并在map中寻找是否有匹配的key
162165
return [records[target- value], index]
163-
records[value] = index # 遍历当前元素,并在map中寻找是否有匹配的key
166+
records[value] = index # 如果没找到匹配对,就把访问过的元素和下标加入到map中
164167
return []
165168
```
166169
(版本二)使用集合
@@ -211,7 +214,7 @@ class Solution:
211214
return [i,j]
212215
```
213216

214-
Go:
217+
### Go:
215218

216219
```go
217220
// 暴力解法
@@ -242,7 +245,7 @@ func twoSum(nums []int, target int) []int {
242245
}
243246
```
244247

245-
Rust
248+
### Rust:
246249

247250
```rust
248251
use std::collections::HashMap;
@@ -263,9 +266,7 @@ impl Solution {
263266
}
264267
}
265268
```
266-
Rust
267-
268-
```
269+
```rust
269270
use std::collections::HashMap;
270271

271272
impl Solution {
@@ -284,7 +285,7 @@ impl Solution {
284285
}
285286
```
286287

287-
Javascript
288+
### Javascript:
288289

289290
```javascript
290291
var twoSum = function (nums, target) {
@@ -299,7 +300,7 @@ var twoSum = function (nums, target) {
299300
};
300301
```
301302

302-
TypeScript:
303+
### TypeScript:
303304

304305
```typescript
305306
function twoSum(nums: number[], target: number): number[] {
@@ -317,7 +318,7 @@ function twoSum(nums: number[], target: number): number[] {
317318
};
318319
```
319320

320-
php
321+
### php:
321322

322323
```php
323324
function twoSum(array $nums, int $target): array
@@ -337,7 +338,8 @@ function twoSum(array $nums, int $target): array
337338
}
338339
```
339340

340-
Swift:
341+
### Swift:
342+
341343
```swift
342344
func twoSum(_ nums: [Int], _ target: Int) -> [Int] {
343345
// 值: 下标
@@ -353,8 +355,8 @@ func twoSum(_ nums: [Int], _ target: Int) -> [Int] {
353355
}
354356
```
355357

358+
### Scala:
356359

357-
Scala:
358360
```scala
359361
object Solution {
360362
// 导入包
@@ -377,7 +379,8 @@ object Solution {
377379
}
378380
```
379381

380-
C#:
382+
### C#:
383+
381384
```csharp
382385
public class Solution {
383386
public int[] TwoSum(int[] nums, int target) {
@@ -396,7 +399,8 @@ public class Solution {
396399
}
397400
```
398401

399-
Dart:
402+
### Dart:
403+
400404
```dart
401405
List<int> twoSum(List<int> nums, int target) {
402406
var tmp = [];
@@ -411,7 +415,8 @@ List<int> twoSum(List<int> nums, int target) {
411415
}
412416
```
413417

414-
C:
418+
### C:
419+
415420
```c
416421

417422

problems/0015.三数之和.md

Lines changed: 32 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,15 @@
2626
[-1, -1, 2]
2727
]
2828

29+
## 算法公开课
2930

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

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

36-
## 哈希解法
35+
## 思路
36+
37+
### 哈希解法
3738

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

@@ -87,7 +88,7 @@ public:
8788
* 空间复杂度: O(n),额外的 set 开销
8889
8990
90-
## 双指针
91+
### 双指针
9192
9293
**其实这道题目使用哈希法并不十分合适**,因为在去重的操作中有很多细节需要注意,在面试中很难直接写出没有bug的代码。
9394
@@ -166,9 +167,9 @@ public:
166167
* 空间复杂度: O(1)
167168

168169

169-
## 去重逻辑的思考
170+
### 去重逻辑的思考
170171

171-
### a的去重
172+
#### a的去重
172173

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

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

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

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

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

211-
### b与c的去重
212+
#### b与c的去重
212213

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

@@ -225,7 +226,7 @@ while (right > left) {
225226
} else {
226227
}
227228
}
228-
```
229+
```
229230

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

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

240241

241-
# 思考题
242+
## 思考题
242243

243244

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

255256
## 其他语言版本
256257

258+
### Java:
257259

258-
Java:
259260
```Java
260261
class Solution {
261262
public List<List<Integer>> threeSum(int[] nums) {
@@ -297,8 +298,9 @@ class Solution {
297298
}
298299
```
299300

300-
Python:
301+
### Python:
301302
(版本一) 双指针
303+
302304
```Python
303305
class Solution:
304306
def threeSum(self, nums: List[int]) -> List[List[int]]:
@@ -366,7 +368,7 @@ class Solution:
366368
return result
367369
```
368370

369-
Go:
371+
### Go:
370372

371373
```Go
372374
func threeSum(nums []int) [][]int {
@@ -407,7 +409,7 @@ func threeSum(nums []int) [][]int {
407409
}
408410
```
409411

410-
javaScript:
412+
### JavaScript:
411413

412414
```js
413415
var threeSum = function(nums) {
@@ -512,7 +514,7 @@ var threeSum = function (nums) {
512514
};
513515
```
514516

515-
TypeScript:
517+
### TypeScript:
516518

517519
```typescript
518520
function threeSum(nums: number[]): number[][] {
@@ -553,7 +555,8 @@ function threeSum(nums: number[]): number[][] {
553555
};
554556
```
555557

556-
ruby:
558+
### Ruby:
559+
557560
```ruby
558561
def is_valid(strs)
559562
symbol_map = {')' => '(', '}' => '{', ']' => '['}
@@ -571,8 +574,8 @@ def is_valid(strs)
571574
end
572575
```
573576

577+
### PHP:
574578

575-
PHP:
576579
```php
577580
class Solution {
578581
/**
@@ -613,7 +616,8 @@ class Solution {
613616
}
614617
```
615618

616-
Swift:
619+
### Swift:
620+
617621
```swift
618622
// 双指针法
619623
func threeSum(_ nums: [Int]) -> [[Int]] {
@@ -654,7 +658,8 @@ func threeSum(_ nums: [Int]) -> [[Int]] {
654658
}
655659
```
656660

657-
Rust:
661+
### Rust:
662+
658663
```Rust
659664
// 哈希解法
660665
use std::collections::HashSet;
@@ -718,7 +723,8 @@ impl Solution {
718723
}
719724
```
720725

721-
C:
726+
### C:
727+
722728
```C
723729
//qsort辅助cmp函数
724730
int cmp(const void* ptr1, const void* ptr2) {
@@ -792,7 +798,8 @@ int** threeSum(int* nums, int numsSize, int* returnSize, int** returnColumnSizes
792798
}
793799
```
794800
795-
C#:
801+
### C#:
802+
796803
```csharp
797804
public class Solution
798805
{
@@ -850,7 +857,8 @@ public class Solution
850857
}
851858
}
852859
```
853-
Scala:
860+
### Scala:
861+
854862
```scala
855863
object Solution {
856864
// 导包
@@ -898,3 +906,4 @@ object Solution {
898906
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
899907
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
900908
</a>
909+

0 commit comments

Comments
 (0)