Skip to content

Commit 3ed64ce

Browse files
Update
1 parent 66e6953 commit 3ed64ce

11 files changed

+161
-101
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,8 @@
124124

125125
## 知识星球精选
126126

127+
* [秋招下半场依然没offer,怎么办?](./problems/知识星球精选/秋招下半场依然没offer.md)
128+
* [合适自己的就是最好的](./problems/知识星球精选/合适自己的就是最好的.md)
127129
* [为什么都说客户端会消失](./problems/知识星球精选/客三消.md)
128130
* [博士转计算机如何找工作](./problems/知识星球精选/博士转行计算机.md)
129131
* [不一样的七夕](./problems/知识星球精选/不一样的七夕.md)
@@ -462,6 +464,8 @@
462464
* [24.两两交换链表中的节点](./problems/0024.两两交换链表中的节点.md)
463465
* [234.回文链表](./problems/0234.回文链表.md)
464466
* [143.重排链表](./problems/0143.重排链表.md)【数组】【双向队列】【直接操作链表】
467+
468+
465469
* [141.环形链表](./problems/0141.环形链表.md)
466470

467471
## 哈希表

problems/0015.三数之和.md

Lines changed: 0 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -354,44 +354,6 @@ def is_valid(strs)
354354
end
355355
```
356356

357-
php:
358-
359-
```php
360-
function threeSum(array $nums): array
361-
{
362-
$result = [];
363-
$length = count($nums);
364-
if ($length < 3) {
365-
return [];
366-
}
367-
sort($nums);
368-
for ($i = 0; $i < $length; $i++) {
369-
// 如果大于0结束
370-
if ($nums[$i] > 0) break;
371-
// 去重
372-
if ($i > 0 && $nums[$i] == $nums[$i - 1]) continue;
373-
$left = $i + 1;
374-
$right = $length - 1;
375-
// 比较
376-
while ($left < $right) {
377-
$sum = $nums[$i] + $nums[$left] + $nums[$right];
378-
if ($sum < 0) {
379-
$left++;
380-
} elseif ($sum > 0) {
381-
$right--;
382-
} else {
383-
array_push($result, [$nums[$i], $nums[$left], $nums[$right]]);
384-
while ($left < $right && $nums[$left] == $nums[$left + 1]) $left++;
385-
while ($left < $right && $nums[$right - 1] == $nums[$right]) $right--;
386-
$left++;
387-
$right--;
388-
}
389-
}
390-
}
391-
392-
return $result;
393-
}
394-
```
395357

396358
PHP:
397359
```php

problems/0018.四数之和.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,8 @@ public:
9090
int left = i + 1;
9191
int right = nums.size() - 1;
9292
while (right > left) {
93-
if (nums[k] + nums[i] + nums[left] + nums[right] > target) {
93+
// nums[k] + nums[i] + nums[left] + nums[right] > target 会溢出
94+
if (nums[k] + nums[i] > target - (nums[left] + nums[right])) {
9495
right--;
9596
} else if (nums[k] + nums[i] + nums[left] + nums[right] < target) {
9697
left++;
@@ -110,8 +111,8 @@ public:
110111
}
111112
return result;
112113
}
113-
114114
};
115+
115116
```
116117

117118

problems/0039.组合总和.md

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

99

10-
## 39. 组合总和
10+
# 39. 组合总和
1111

1212
[力扣题目链接](https://leetcode-cn.com/problems/combination-sum/)
1313

@@ -37,21 +37,21 @@ candidates 中的数字可以无限制重复被选取。
3737
  [3,5]
3838
]
3939

40-
## 思路
40+
# 思路
4141

4242
[B站视频讲解-组合总和](https://www.bilibili.com/video/BV1KT4y1M7HJ)
4343

4444

4545
题目中的**无限制重复被选取,吓得我赶紧想想 出现0 可咋办**,然后看到下面提示:1 <= candidates[i] <= 200,我就放心了。
4646

47-
本题和[回溯算法:求组合问题!](https://programmercarl.com/0077.组合.html)[回溯算法:求组合总和!](https://programmercarl.com/0216.组合总和III.html)和区别是:本题没有数量要求,可以无限重复,但是有总和的限制,所以间接的也是有个数的限制。
47+
本题和[77.组合](https://programmercarl.com/0077.组合.html)[216.组合总和III](https://programmercarl.com/0216.组合总和III.html)和区别是:本题没有数量要求,可以无限重复,但是有总和的限制,所以间接的也是有个数的限制。
4848

4949
本题搜索的过程抽象成树形结构如下:
5050

5151
![39.组合总和](https://img-blog.csdnimg.cn/20201223170730367.png)
5252
注意图中叶子节点的返回条件,因为本题没有组合数量要求,仅仅是总和的限制,所以递归没有层数的限制,只要选取的元素总和超过target,就返回!
5353

54-
而在[回溯算法:求组合问题!](https://programmercarl.com/0077.组合.html)[回溯算法:求组合总和!](https://programmercarl.com/0216.组合总和III.html) 中都可以知道要递归K层,因为要取k个元素的组合。
54+
而在[77.组合](https://programmercarl.com/0077.组合.html)[216.组合总和III](https://programmercarl.com/0216.组合总和III.html) 中都可以知道要递归K层,因为要取k个元素的组合。
5555

5656
## 回溯三部曲
5757

@@ -65,9 +65,9 @@ candidates 中的数字可以无限制重复被选取。
6565

6666
**本题还需要startIndex来控制for循环的起始位置,对于组合问题,什么时候需要startIndex呢?**
6767

68-
我举过例子,如果是一个集合来求组合的话,就需要startIndex,例如:[回溯算法:求组合问题!](https://programmercarl.com/0077.组合.html)[回溯算法:求组合总和!](https://programmercarl.com/0216.组合总和III.html)
68+
我举过例子,如果是一个集合来求组合的话,就需要startIndex,例如:[77.组合](https://programmercarl.com/0077.组合.html)[216.组合总和III](https://programmercarl.com/0216.组合总和III.html)
6969

70-
如果是多个集合取组合,各个集合之间相互不影响,那么就不用startIndex,例如:[回溯算法:电话号码的字母组合](https://programmercarl.com/0017.电话号码的字母组合.html)
70+
如果是多个集合取组合,各个集合之间相互不影响,那么就不用startIndex,例如:[17.电话号码的字母组合](https://programmercarl.com/0017.电话号码的字母组合.html)
7171

7272
**注意以上我只是说求组合的情况,如果是排列问题,又是另一套分析的套路,后面我再讲解排列的时候就重点介绍**
7373

@@ -103,7 +103,7 @@ if (sum == target) {
103103

104104
单层for循环依然是从startIndex开始,搜索candidates集合。
105105

106-
**注意本题和[回溯算法:求组合问题!](https://programmercarl.com/0077.组合.html)[回溯算法:求组合总和!](https://programmercarl.com/0216.组合总和III.html)的一个区别是:本题元素为可重复选取的**
106+
**注意本题和[77.组合](https://programmercarl.com/0077.组合.html)[216.组合总和III](https://programmercarl.com/0216.组合总和III.html)的一个区别是:本题元素为可重复选取的**
107107

108108
如何重复选取呢,看代码,注释部分:
109109

@@ -211,16 +211,16 @@ public:
211211
};
212212
```
213213

214-
## 总结
214+
# 总结
215215

216-
本题和我们之前讲过的[回溯算法:求组合问题!](https://programmercarl.com/0077.组合.html)[回溯算法:求组合总和!](https://programmercarl.com/0216.组合总和III.html)有两点不同:
216+
本题和我们之前讲过的[77.组合](https://programmercarl.com/0077.组合.html)[216.组合总和III](https://programmercarl.com/0216.组合总和III.html)有两点不同:
217217

218218
* 组合没有数量要求
219219
* 元素可无限重复选取
220220

221221
针对这两个问题,我都做了详细的分析。
222222

223-
并且给出了对于组合问题,什么时候用startIndex,什么时候不用,并用[回溯算法:电话号码的字母组合](https://programmercarl.com/0017.电话号码的字母组合.html)做了对比。
223+
并且给出了对于组合问题,什么时候用startIndex,什么时候不用,并用[17.电话号码的字母组合](https://programmercarl.com/0017.电话号码的字母组合.html)做了对比。
224224

225225
最后还给出了本题的剪枝优化,这个优化如果是初学者的话并不容易想到。
226226

@@ -232,10 +232,10 @@ public:
232232

233233

234234

235-
## 其他语言版本
235+
# 其他语言版本
236236

237237

238-
Java
238+
## Java
239239
```Java
240240
// 剪枝优化
241241
class Solution {
@@ -264,7 +264,7 @@ class Solution {
264264
}
265265
```
266266

267-
Python
267+
## Python
268268
```python3
269269
class Solution:
270270
def combinationSum(self, candidates: List[int], target: int) -> List[List[int]]:
@@ -284,10 +284,9 @@ class Solution:
284284
backtrack(candidates,target,0,0)
285285
return res
286286
```
287-
Go:
288287

289-
290-
> 主要在于递归中传递下一个数字
288+
## Go
289+
主要在于递归中传递下一个数字
291290

292291
```go
293292
func combinationSum(candidates []int, target int) [][]int {
@@ -319,7 +318,8 @@ func backtracking(startIndex,sum,target int,candidates,trcak []int,res *[][]int)
319318

320319
}
321320
```
322-
JavaScript:
321+
322+
## JavaScript:
323323

324324
```js
325325
var combinationSum = function(candidates, target) {
@@ -346,7 +346,7 @@ var combinationSum = function(candidates, target) {
346346
};
347347
```
348348

349-
C:
349+
## C
350350
```c
351351
int* path;
352352
int pathTop;

problems/0040.组合总和II.md

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
> 这篇可以说是全网把组合问题如何去重,讲的最清晰的了!
1111
12-
## 40.组合总和II
12+
# 40.组合总和II
1313

1414
[力扣题目链接](https://leetcode-cn.com/problems/combination-sum-ii/)
1515

@@ -39,7 +39,7 @@ candidates 中的每个数字在每个组合中只能使用一次。
3939
  [5]
4040
]
4141

42-
## 思路
42+
# 思路
4343

4444
**如果对回溯算法基础还不了解的话,我还特意录制了一期视频:[带你学透回溯算法(理论篇)](https://www.bilibili.com/video/BV1cy4y167mM/)** 可以结合题解和视频一起看,希望对大家理解回溯算法有所帮助。
4545

@@ -157,7 +157,6 @@ for (int i = startIndex; i < candidates.size() && sum + candidates[i] <= target;
157157

158158
**注意sum + candidates[i] <= target为剪枝操作,在[39.组合总和](https://mp.weixin.qq.com/s/FLg8G6EjVcxBjwCbzpACPw)有讲解过!**
159159

160-
## C++代码
161160

162161
回溯三部曲分析完了,整体C++代码如下:
163162

@@ -242,7 +241,7 @@ public:
242241
243242
```
244243

245-
## 总结
244+
# 总结
246245

247246
本题同样是求组合总和,但就是因为其数组candidates有重复元素,而要求不能有重复的组合,所以相对于[39.组合总和](https://programmercarl.com/0039.组合总和.html)难度提升了不少。
248247

@@ -254,10 +253,10 @@ public:
254253

255254

256255

257-
## 其他语言版本
256+
# 其他语言版本
258257

259258

260-
Java
259+
## Java
261260
```Java
262261
class Solution {
263262
List<List<Integer>> lists = new ArrayList<>();
@@ -295,7 +294,8 @@ class Solution {
295294
}
296295
}
297296
```
298-
Python:
297+
298+
## Python
299299
```python
300300
class Solution:
301301
def combinationSum2(self, candidates: List[int], target: int) -> List[List[int]]:
@@ -315,10 +315,9 @@ class Solution:
315315
backtrack(candidates,target,0,0)
316316
return res
317317
```
318-
Go:
319-
320318

321-
> 主要在于如何在回溯中去重
319+
## Go:
320+
主要在于如何在回溯中去重
322321

323322
```go
324323
func combinationSum2(candidates []int, target int) [][]int {
@@ -359,7 +358,8 @@ func backtracking(startIndex,sum,target int,candidates,trcak []int,res *[][]int,
359358
}
360359
}
361360
```
362-
javaScript:
361+
362+
## javaScript:
363363

364364
```js
365365
/**
@@ -392,7 +392,8 @@ var combinationSum2 = function(candidates, target) {
392392
}
393393
};
394394
```
395-
C:
395+
## C
396+
396397
```c
397398
int* path;
398399
int pathTop;

problems/0349.两个数组的交集.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ class Solution {
117117
}
118118
```
119119

120-
Python
120+
Python3
121121
```python
122122
class Solution:
123123
def intersection(self, nums1: List[int], nums2: List[int]) -> List[int]:

problems/0454.四数相加II.md

Lines changed: 1 addition & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -122,13 +122,6 @@ Python:
122122
```python
123123
class Solution(object):
124124
def fourSumCount(self, nums1, nums2, nums3, nums4):
125-
"""
126-
:type nums1: List[int]
127-
:type nums2: List[int]
128-
:type nums3: List[int]
129-
:type nums4: List[int]
130-
:rtype: int
131-
"""
132125
# use a dict to store the elements in nums1 and nums2 and their sum
133126
hashmap = dict()
134127
for n1 in nums1:
@@ -147,27 +140,8 @@ class Solution(object):
147140
count += hashmap[key]
148141
return count
149142

150-
# 下面这个写法更为简洁,但是表达的是同样的算法
151-
# class Solution:
152-
# def fourSumCount(self, nums1: List[int], nums2: List[int], nums3: List[int], nums4: List[int]) -> int:
153-
# from collections import defaultdict
154-
155-
# hashmap = defaultdict(int)
156-
157-
# for x1 in nums1:
158-
# for x2 in nums2:
159-
# hashmap[x1+x2] += 1
160-
161-
# count=0
162-
# for x3 in nums3:
163-
# for x4 in nums4:
164-
# key = 0 - x3 - x4
165-
# value = hashmap[key] # 若差值(key)不存在,则value被赋值0
166-
# count += value
167-
# return count
168-
169-
```
170143

144+
```
171145

172146
Go:
173147
```go

problems/0496.下一个更大元素I.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,4 +271,27 @@ func nextGreaterElement(nums1 []int, nums2 []int) []int {
271271
}
272272
```
273273

274+
JavaScript:
275+
276+
```JS
277+
var nextGreaterElement = function (nums1, nums2) {
278+
let stack = [];
279+
let map = new Map();
280+
for (let i = 0; i < nums2.length; i++) {
281+
while (stack.length && nums2[i] > nums2[stack[stack.length - 1]]) {
282+
let index = stack.pop();
283+
map.set(nums2[index], nums2[i]);
284+
}
285+
stack.push(i);
286+
}
287+
288+
let res = [];
289+
for (let j = 0; j < nums1.length; j++) {
290+
res[j] = map.get(nums1[j]) || -1;
291+
}
292+
293+
return res;
294+
};
295+
```
296+
274297
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码.jpg width=450> </img></div>

problems/0673.最长递增子序列的个数.md

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

2929
# 思路
3030

31-
这道题可以说是 300.最长上升子序列 的进阶版本
31+
这道题可以说是 [300.最长上升子序列](https://programmercarl.com/0300.最长上升子序列.html) 的进阶版本
3232

3333
1. 确定dp数组(dp table)以及下标的含义
3434

0 commit comments

Comments
 (0)