Skip to content

Commit 5257958

Browse files
committed
2 parents 4672df4 + 9fc2c9c commit 5257958

File tree

266 files changed

+12164
-3957
lines changed

Some content is hidden

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

266 files changed

+12164
-3957
lines changed

README.md

Lines changed: 34 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
## 一些闲话:
1+
👉 推荐 [在线阅读](http://programmercarl.com/) (Github在国内访问经常不稳定)
2+
👉 推荐 [Gitee同步](https://gitee.com/programmercarl/leetcode-master)
23

34
> 1. **介绍**:本项目是一套完整的刷题计划,旨在帮助大家少走弯路,循序渐进学算法,[关注作者](#关于作者)
45
> 2. **PDF版本**[「代码随想录」算法精讲 PDF 版本](https://mp.weixin.qq.com/s/RsdcQ9umo09R6cfnwXZlrQ)
@@ -87,6 +88,7 @@
8788

8889
* 编程语言
8990
* [C++面试&C++学习指南知识点整理](https://github.com/youngyangyang04/TechCPP)
91+
9092
* 项目
9193
* [基于跳表的轻量级KV存储引擎](https://github.com/youngyangyang04/Skiplist-CPP)
9294
* [Nosql数据库注入攻击系统](https://github.com/youngyangyang04/NoSQLAttack)
@@ -95,6 +97,7 @@
9597
* [看了这么多代码,谈一谈代码风格!](./problems/前序/代码风格.md)
9698
* [力扣上的代码想在本地编译运行?](./problems/前序/力扣上的代码想在本地编译运行?.md)
9799
* [什么是核心代码模式,什么又是ACM模式?](./problems/前序/什么是核心代码模式,什么又是ACM模式?.md)
100+
98101
* 工具
99102
* [一站式vim配置](https://github.com/youngyangyang04/PowerVim)
100103
* [保姆级Git入门教程,万字详解](https://mp.weixin.qq.com/s/Q_O0ey4C9tryPZaZeJocbA)
@@ -119,27 +122,33 @@
119122
* [递归算法的时间与空间复杂度分析!](./problems/前序/递归算法的时间与空间复杂度分析.md)
120123
* [刷了这么多题,你了解自己代码的内存消耗么?](./problems/前序/刷了这么多题,你了解自己代码的内存消耗么?.md)
121124

122-
(持续更新中.....)
123-
124125
## 知识星球精选
125126

126-
1. [选择方向的时候,我也迷茫了](https://mp.weixin.qq.com/s/ZCzFiAHZHLqHPLJQXNm75g)
127-
2. [刷题就用库函数了,怎么了?](https://mp.weixin.qq.com/s/6K3_OSaudnHGq2Ey8vqYfg)
128-
3. [关于实习,大家可能有点迷茫!](https://mp.weixin.qq.com/s/xcxzi7c78kQGjvZ8hh7taA)
129-
4. [马上秋招了,慌得很!](https://mp.weixin.qq.com/s/7q7W8Cb2-a5U5atZdOnOFA)
130-
5. [Carl看了上百份简历,总结了这些!](https://mp.weixin.qq.com/s/sJa87MZD28piCOVMFkIbwQ)
131-
6. [面试中遇到了发散性问题.....](https://mp.weixin.qq.com/s/SSonDxi2pjkSVwHNzZswng)
132-
7. [英语到底重不重要!](https://mp.weixin.qq.com/s/1PRZiyF_-TVA-ipwDNjdKw)
133-
8. [计算机专业要不要读研!](https://mp.weixin.qq.com/s/c9v1L3IjqiXtkNH7sOMAdg)
134-
9. [秋招和提前批都越来越提前了....](https://mp.weixin.qq.com/s/SNFiRDx8CKyjhTPlys6ywQ)
135-
10. [你的简历里「专业技能」写的够专业么?](https://mp.weixin.qq.com/s/bp6y-e5FVN28H9qc8J9zrg)
136-
11. [对于秋招,实习生也有烦恼....](https://mp.weixin.qq.com/s/ka07IPryFnfmIjByFFcXDg)
137-
12. [华为提前批已经开始了.....](https://mp.weixin.qq.com/s/OC35QDG8pn5OwLpCxieStw)
138-
13. [大厂新人培养体系应该是什么样的?](https://mp.weixin.qq.com/s/WBaPCosOljB5NEkFL2GhOQ)
127+
* [为什么都说客户端会消失](./problems/知识星球精选/客三消.md)
128+
* [博士转计算机如何找工作](./problems/知识星球精选/博士转行计算机.md)
129+
* [不一样的七夕](./problems/知识星球精选/不一样的七夕.md)
130+
* [HR面注意事项](./problems/知识星球精选/HR面注意事项.md)
131+
* [刷题攻略要刷两遍!](./problems/知识星球精选/刷题攻略要刷两遍.md)
132+
* [秋招进行中的迷茫与焦虑......](./problems/知识星球精选/秋招进行中的迷茫与焦虑.md)
133+
* [大厂新人培养体系应该是什么样的?](./problems/知识星球精选/大厂新人培养体系.md)
134+
* [你的简历里「专业技能」写的够专业么?](./problems/知识星球精选/专业技能可以这么写.md)
135+
* [Carl看了上百份简历,总结了这些!](./problems/知识星球精选/写简历的一些问题.md)
136+
* [备战2022届秋招](./problems/知识星球精选/备战2022届秋招.md)
137+
* [技术不太好,如果选择方向](./problems/知识星球精选/技术不好如何选择技术方向.md)
138+
* [刷题要不要使用库函数](./problems/知识星球精选/刷力扣用不用库函数.md)
139+
* [关于实习的几点问题](./problems/知识星球精选/关于实习大家的疑问.md)
140+
* [面试中遇到了发散性问题,怎么办?](./problems/知识星球精选/面试中发散性问题.md)
141+
* [英语到底重不重要!](./problems/知识星球精选/英语到底重不重要.md)
142+
* [计算机专业要不要读研!](./problems/知识星球精选/要不要考研.md)
143+
* [关于提前批的一些建议](./problems/知识星球精选/关于提前批的一些建议.md)
144+
* [已经在实习的录友要如何准备秋招](./problems/知识星球精选/如何权衡实习与秋招复习.md)
145+
* [华为提前批已经开始了](./problems/知识星球精选/提前批已经开始了.md)
139146

140147
## 杂谈
141148

149+
* [「代码随想录」刷题网站上线](https://mp.weixin.qq.com/s/-6rd_g7LrVD1fuKBYk2tXQ)
142150
* [LeetCode-Master上榜了](https://mp.weixin.qq.com/s/wZRTrA9Rbvgq1yEkSw4vfQ)
151+
* [上榜之后,都有哪些变化?](https://mp.weixin.qq.com/s/VJBV0qSBthjnbbmW-lctLA)
143152
* [大半年过去了......](https://mp.weixin.qq.com/s/lubfeistPxBLSQIe5XYg5g)
144153
* [一万录友在B站学算法!](https://mp.weixin.qq.com/s/Vzq4zkMZY7erKeu0fqGLgw)
145154

@@ -241,7 +250,7 @@
241250
15. [二叉树:以为使用了递归,其实还隐藏着回溯](./problems/二叉树中递归带着回溯.md)
242251
16. [二叉树:做了这么多题目了,我的左叶子之和是多少?](./problems/0404.左叶子之和.md)
243252
17. [二叉树:我的左下角的值是多少?](./problems/0513.找树左下角的值.md)
244-
18. [二叉树:递归函数究竟什么时候需要返回值,什么时候不要返回值?](./problems/0112.路径总和.md)
253+
18. [二叉树:路径总和](./problems/0112.路径总和.md)
245254
19. [二叉树:构造二叉树登场!](./problems/0106.从中序与后序遍历序列构造二叉树.md)
246255
20. [二叉树:构造一棵最大的二叉树](./problems/0654.最大二叉树.md)
247256
21. [本周小结!(二叉树系列三)](./problems/周总结/20201010二叉树周末总结.md)
@@ -409,6 +418,7 @@
409418
2. [单调栈:下一个更大元素I](./problems/0496.下一个更大元素I.md)
410419
3. [单调栈:下一个更大元素II](./problems/0503.下一个更大元素II.md)
411420
4. [单调栈:接雨水](./problems/0042.接雨水.md)
421+
5. [单调栈:柱状图中最大的矩形](./problems/0084.柱状图中最大的矩形.md)
412422

413423
(持续更新中....)
414424

@@ -451,7 +461,6 @@
451461
* [24.两两交换链表中的节点](./problems/0024.两两交换链表中的节点.md)
452462
* [234.回文链表](./problems/0234.回文链表.md)
453463
* [143.重排链表](./problems/0143.重排链表.md)【数组】【双向队列】【直接操作链表】
454-
* [234.回文链表](./problems/0234.回文链表.md)
455464
* [141.环形链表](./problems/0141.环形链表.md)
456465

457466
## 哈希表
@@ -467,8 +476,14 @@
467476
* [100.相同的树](./problems/0100.相同的树.md) 同101.对称二叉树 一个思路
468477
* [116.填充每个节点的下一个右侧节点指针](./problems/0116.填充每个节点的下一个右侧节点指针.md)
469478

479+
## 回溯算法
480+
481+
* [52.N皇后II](./problems/0052.N皇后II.md)
482+
483+
470484
## 贪心
471485
* [649.Dota2参议院](./problems/0649.Dota2参议院.md) 有难度
486+
* [1221.分割平衡字符](./problems/1221.分割平衡字符串.md) 简单贪心
472487

473488
## 动态规划
474489
* [5.最长回文子串](./problems/0005.最长回文子串.md)[647.回文子串](https://mp.weixin.qq.com/s/2WetyP6IYQ6VotegepVpEw) 差不多是一样的
@@ -478,6 +493,7 @@
478493
## 图论
479494
* [463.岛屿的周长](./problems/0463.岛屿的周长.md) (模拟)
480495
* [841.钥匙和房间](./problems/0841.钥匙和房间.md) 【有向图】dfs,bfs都可以
496+
* [127.单词接龙](./problems/0127.单词接龙.md) 广搜
481497

482498
## 并查集
483499
* [684.冗余连接](./problems/0684.冗余连接.md) 【并查集基础题目】

problems/0001.两数之和.md

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

1010
## 1. 两数之和
1111

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

1414
给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标。
1515

@@ -29,10 +29,10 @@ https://leetcode-cn.com/problems/two-sum/
2929
很明显暴力的解法是两层for循环查找,时间复杂度是O(n^2)。
3030

3131
建议大家做这道题目之前,先做一下这两道
32-
* [242. 有效的字母异位词](https://mp.weixin.qq.com/s/ffS8jaVFNUWyfn_8T31IdA)
33-
* [349. 两个数组的交集](https://mp.weixin.qq.com/s/aMSA5zrp3jJcLjuSB0Es2Q)
32+
* [242. 有效的字母异位词](https://www.programmercarl.com/0242.有效的字母异位词.html)
33+
* [349. 两个数组的交集](https://www.programmercarl.com/0349.两个数组的交集.html)
3434

35-
[242. 有效的字母异位词](https://mp.weixin.qq.com/s/ffS8jaVFNUWyfn_8T31IdA) 这道题目是用数组作为哈希表来解决哈希问题,[349. 两个数组的交集](https://mp.weixin.qq.com/s/aMSA5zrp3jJcLjuSB0Es2Q)这道题目是通过set作为哈希表来解决哈希问题。
35+
[242. 有效的字母异位词](https://www.programmercarl.com/0242.有效的字母异位词.html) 这道题目是用数组作为哈希表来解决哈希问题,[349. 两个数组的交集](https://www.programmercarl.com/0349.两个数组的交集.html)这道题目是通过set作为哈希表来解决哈希问题。
3636

3737
本题呢,则要使用map,那么来看一下使用数组和set来做哈希法的局限。
3838

@@ -51,7 +51,7 @@ C++中map,有三种类型:
5151

5252
std::unordered_map 底层实现为哈希表,std::map 和std::multimap 的底层实现是红黑树。
5353

54-
同理,std::map 和std::multimap 的key也是有序的(这个问题也经常作为面试题,考察对语言容器底层的理解)。 更多哈希表的理论知识请看[关于哈希表,你该了解这些!](https://mp.weixin.qq.com/s/RSUANESA_tkhKhYe3ZR8Jg)
54+
同理,std::map 和std::multimap 的key也是有序的(这个问题也经常作为面试题,考察对语言容器底层的理解)。 更多哈希表的理论知识请看[关于哈希表,你该了解这些!](https://www.programmercarl.com/哈希表理论基础.html)
5555

5656
**这道题目中并不需要key有序,选择std::unordered_map 效率更高!**
5757

@@ -62,7 +62,7 @@ std::unordered_map 底层实现为哈希表,std::map 和std::multimap 的底
6262

6363
C++代码:
6464

65-
```C++
65+
```CPP
6666
class Solution {
6767
public:
6868
vector<int> twoSum(vector<int>& nums, int target) {
@@ -110,13 +110,14 @@ Python:
110110
```python
111111
class Solution:
112112
def twoSum(self, nums: List[int], target: int) -> List[int]:
113-
hashmap={}
114-
for ind,num in enumerate(nums):
115-
hashmap[num] = ind
116-
for i,num in enumerate(nums):
117-
j = hashmap.get(target - num)
118-
if j is not None and i!=j:
119-
return [i,j]
113+
records = dict()
114+
115+
# 用枚举更方便,就不需要通过索引再去取当前位置的值
116+
for idx, val in enumerate(nums):
117+
if target - val not in records:
118+
records[val] = idx
119+
else:
120+
return [records[target - val], idx] # 如果存在就返回字典记录索引和当前索引
120121
```
121122

122123

@@ -205,9 +206,51 @@ function twoSum(array $nums, int $target): array
205206
}
206207
```
207208

209+
Swift:
210+
```swift
211+
func twoSum(_ nums: [Int], _ target: Int) -> [Int] {
212+
var res = [Int]()
213+
var dict = [Int : Int]()
214+
for i in 0 ..< nums.count {
215+
let other = target - nums[i]
216+
if dict.keys.contains(other) {
217+
res.append(i)
218+
res.append(dict[other]!)
219+
return res
220+
}
221+
dict[nums[i]] = i
222+
}
223+
return res
224+
}
225+
```
226+
227+
PHP:
228+
```php
229+
class Solution {
230+
/**
231+
* @param Integer[] $nums
232+
* @param Integer $target
233+
* @return Integer[]
234+
*/
235+
function twoSum($nums, $target) {
236+
if (count($nums) == 0) {
237+
return [];
238+
}
239+
$table = [];
240+
for ($i = 0; $i < count($nums); $i++) {
241+
$temp = $target - $nums[$i];
242+
if (isset($table[$temp])) {
243+
return [$table[$temp], $i];
244+
}
245+
$table[$nums[$i]] = $i;
246+
}
247+
return [];
248+
}
249+
}
250+
```
208251

209252
-----------------------
210253
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
211254
* B站视频:[代码随想录](https://space.bilibili.com/525438321)
212255
* 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ)
213-
<div align="center"><img src=../pics/公众号.png width=450 alt=> </img></div>
256+
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码.jpg width=450> </img></div>

problems/0005.最长回文子串.md

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
# 5.最长回文子串
1212

13-
题目链接:https://leetcode-cn.com/problems/longest-palindromic-substring/
13+
[力扣题目链接](https://leetcode-cn.com/problems/longest-palindromic-substring/)
1414

1515
给你一个字符串 s,找到 s 中最长的回文子串。
1616

@@ -30,11 +30,11 @@
3030
示例 4:
3131
* 输入:s = "ac"
3232
* 输出:"a"
33-
 
33+
3434

3535
# 思路
3636

37-
本题和[647.回文子串](https://mp.weixin.qq.com/s/2WetyP6IYQ6VotegepVpEw) 差不多是一样的,但647.回文子串更基本一点,建议可以先做647.回文子串
37+
本题和[647.回文子串](https://programmercarl.com/0647.回文子串.html) 差不多是一样的,但647.回文子串更基本一点,建议可以先做647.回文子串
3838

3939
## 暴力解法
4040

@@ -67,7 +67,7 @@
6767

6868
以上三种情况分析完了,那么递归公式如下:
6969

70-
```C++
70+
```CPP
7171
if (s[i] == s[j]) {
7272
if (j - i <= 1) { // 情况一 和 情况二
7373
dp[i][j] = true;
@@ -81,7 +81,7 @@ if (s[i] == s[j]) {
8181

8282
在得到[i,j]区间是否是回文子串的时候,直接保存最长回文子串的左边界和右边界,代码如下:
8383

84-
```C++
84+
```CPP
8585
if (s[i] == s[j]) {
8686
if (j - i <= 1) { // 情况一 和 情况二
8787
dp[i][j] = true;
@@ -120,7 +120,7 @@ dp[i + 1][j - 1] 在 dp[i][j]的左下角,如图:
120120

121121
代码如下:
122122

123-
```C++
123+
```CPP
124124
for (int i = s.size() - 1; i >= 0; i--) { // 注意遍历顺序
125125
for (int j = i; j < s.size(); j++) {
126126
if (s[i] == s[j]) {
@@ -150,7 +150,7 @@ for (int i = s.size() - 1; i >= 0; i--) { // 注意遍历顺序
150150

151151
以上分析完毕,C++代码如下:
152152

153-
```C++
153+
```CPP
154154
class Solution {
155155
public:
156156
string longestPalindrome(string s) {
@@ -181,7 +181,7 @@ public:
181181
```
182182
以上代码是为了凸显情况一二三,当然是可以简洁一下的,如下:
183183

184-
```C++
184+
```CPP
185185
class Solution {
186186
public:
187187
string longestPalindrome(string s) {
@@ -226,7 +226,7 @@ public:
226226
227227
**这两种情况可以放在一起计算,但分别计算思路更清晰,我倾向于分别计算**,代码如下:
228228
229-
```C++
229+
```CPP
230230
class Solution {
231231
public:
232232
int left = 0;
@@ -270,6 +270,23 @@ public:
270270
## Python
271271

272272
```python
273+
class Solution:
274+
def longestPalindrome(self, s: str) -> str:
275+
dp = [[False] * len(s) for _ in range(len(s))]
276+
maxlenth = 0
277+
left = 0
278+
right = 0
279+
for i in range(len(s) - 1, -1, -1):
280+
for j in range(i, len(s)):
281+
if s[j] == s[i]:
282+
if j - i <= 1 or dp[i + 1][j - 1]:
283+
dp[i][j] = True
284+
if dp[i][j] and j - i + 1 > maxlenth:
285+
maxlenth = j - i + 1
286+
left = i
287+
right = j
288+
return s[left:right + 1]
289+
273290
```
274291

275292
## Go
@@ -286,6 +303,6 @@ public:
286303
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
287304
* B站视频:[代码随想录](https://space.bilibili.com/525438321)
288305
* 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ)
289-
<div align="center"><img src=../pics/公众号.png width=450 alt=> </img></div>
290306

291307

308+
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码.jpg width=450> </img></div>

0 commit comments

Comments
 (0)