Skip to content

Commit d1b4d95

Browse files
Merge branch 'youngyangyang04:master' into master
2 parents b85d2ef + 8f760d0 commit d1b4d95

File tree

238 files changed

+8192
-1467
lines changed

Some content is hidden

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

238 files changed

+8192
-1467
lines changed

.DS_Store

8 KB
Binary file not shown.

README.md

Lines changed: 14 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@
44
👉 推荐 [Gitee同步](https://gitee.com/programmercarl/leetcode-master)
55

66
> 1. **介绍**:本项目是一套完整的刷题计划,旨在帮助大家少走弯路,循序渐进学算法,[关注作者](#关于作者)
7-
> 2. **PDF版本**[「代码随想录」算法精讲 PDF 版本](https://mp.weixin.qq.com/s/NF7NqTD9DU505S6nvR_5Wg)
8-
> 3. **刷题顺序** : README已经将刷题顺序排好了,按照顺序一道一道刷就可以。
9-
> 4. **学习社区** : 一起学习打卡/面试技巧/如何选择offer/大厂内推/职场规则/简历修改/技术分享/程序人生。欢迎加入[「代码随想录」知识星球](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ)
10-
> 5. **提交代码**:本项目统一使用C++语言进行讲解,但已经有Java、Python、Go、JavaScript等等多语言版本,感谢[这里的每一位贡献者](https://github.com/youngyangyang04/leetcode-master/graphs/contributors),如果你也想贡献代码点亮你的头像,[点击这里](https://mp.weixin.qq.com/s/tqCxrMEU-ajQumL1i8im9A)了解提交代码的方式。
11-
> 6. **转载须知** :以下所有文章皆为我([程序员Carl](https://github.com/youngyangyang04))的原创。引用本项目文章请注明出处,发现恶意抄袭或搬运,会动用法律武器维护自己的权益。让我们一起维护一个良好的技术创作环境!
7+
> 2. **PDF版本**[「代码随想录」算法精讲 PDF 版本](https://programmercarl.com/other/algo_pdf.html)
8+
> 3. **最强八股文:**[代码随想录知识星球精华PDF](https://www.programmercarl.com/other/kstar_baguwen.html)
9+
> 4. **刷题顺序** : README已经将刷题顺序排好了,按照顺序一道一道刷就可以。
10+
> 5. **学习社区** : 一起学习打卡/面试技巧/如何选择offer/大厂内推/职场规则/简历修改/技术分享/程序人生。欢迎加入[「代码随想录」知识星球](https://programmercarl.com/other/kstar.html)
11+
> 6. **提交代码**:本项目统一使用C++语言进行讲解,但已经有Java、Python、Go、JavaScript等等多语言版本,感谢[这里的每一位贡献者](https://github.com/youngyangyang04/leetcode-master/graphs/contributors),如果你也想贡献代码点亮你的头像,[点击这里](https://mp.weixin.qq.com/s/tqCxrMEU-ajQumL1i8im9A)了解提交代码的方式。
12+
> 7. **转载须知** :以下所有文章皆为我([程序员Carl](https://github.com/youngyangyang04))的原创。引用本项目文章请注明出处,发现恶意抄袭或搬运,会动用法律武器维护自己的权益。让我们一起维护一个良好的技术创作环境!
1213
1314
<p align="center">
1415
<a href="programmercarl.com" target="_blank">
@@ -22,7 +23,7 @@
2223
<a href="https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ"><img src="https://img.shields.io/badge/知识星球-代码随想录-blue" alt=""></a>
2324
</p>
2425

25-
<p align="center"><strong>《代码随想录》正式出版啦!!录友专属福利,点击下方可以享五折优惠!详细可以<a href="programmercarl.com/other/publish.html">点击这里</a></strong></p>
26+
<p align="center"><strong>《代码随想录》正式出版啦!!录友专属福利,点击下方可以享五折优惠!详细可以<a href="https://programmercarl.com/other/publish.html">点击这里</a></strong></p>
2627

2728
<p align="center">
2829
<a href="https://union-click.jd.com/jdc?e=&p=JF8BAMQJK1olXg8EUVhVCkkWAV8IGV8WVAICU24ZVxNJXF9RXh5UHw0cSgYYXBcIWDoXSQVJQwYAUF1UDEsQHDZNRwYlX0B9A1cfakpyYBkSRj4QKFBUEEAfaEcbM244GFIXWQYAUV5VOHsXBF9edVsUXAcDVVtdDUgnAl8IHFkdXw8KUl5fDkgRM2gIEmtIFVpKAxVtOHsUM184G2sWbURsVApfAR8XA2sLSw8cWA8LUw1ZCElHAmhdTAxGW1YBUlxtCkoWB2Y4" target="_blank">
@@ -88,8 +89,7 @@
8889

8990
## 前序
9091

91-
* [「代码随想录」后序安排](https://mp.weixin.qq.com/s/4eeGJREy6E-v6D7cR_5A4g)
92-
* [「代码随想录」学习社区](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ)
92+
* [「代码随想录」学习社区](https://programmercarl.com/other/kstar.html)
9393

9494

9595
* 编程语言
@@ -123,7 +123,7 @@
123123

124124
* 算法性能分析
125125
* [关于时间复杂度,你不知道的都在这里!](./problems/前序/关于时间复杂度,你不知道的都在这里!.md)
126-
* [$O(n)$的算法居然超时了,此时的n究竟是多大?](./problems/前序/On的算法居然超时了,此时的n究竟是多大?.md)
126+
* [O(n)的算法居然超时了,此时的n究竟是多大?](./problems/前序/On的算法居然超时了,此时的n究竟是多大?.md)
127127
* [通过一道面试题目,讲一讲递归算法的时间复杂度!](./problems/前序/通过一道面试题目,讲一讲递归算法的时间复杂度!.md)
128128
* [本周小结!(算法性能分析系列一)](./problems/周总结/20201210复杂度分析周末总结.md)
129129
* [关于空间复杂度,可能有几个疑问?](./problems/前序/关于空间复杂度,可能有几个疑问?.md)
@@ -564,14 +564,14 @@
564564

565565
# 关于作者
566566

567-
大家好,我是程序员Carl,哈工大师兄,ACM 校赛、黑龙江省赛、东北四省赛金牌、亚洲区域赛铜牌获得者,先后在腾讯和百度从事后端技术研发,CSDN博客专家。对算法和C++后端技术有一定的见解,利用工作之余重新刷leetcode。
567+
大家好,我是程序员Carl,哈工大师兄,《代码随想录》作者,先后在腾讯和百度从事后端技术研发,CSDN博客专家。对算法和C++后端技术有一定的见解,利用工作之余重新刷leetcode。
568568

569-
加入刷题微信群,备注:「个人简单介绍」 + 组队刷题
569+
加入「代码随想录」刷题小分队(微信群),可以扫下方二维码加我微信。
570570

571-
也欢迎与我交流,备注:「个人简单介绍」 + 交流,围观朋友圈,做点赞之交(备注没有自我介绍不通过哦)
571+
如果是已工作,备注:姓名-城市-岗位-组队刷题。如果学生,备注:姓名-学校-年级-组队刷题。**备注没有自我介绍不通过哦**
572+
573+
<div align="center"><img src="https://code-thinking-1253855093.file.myqcloud.com/pics/20220102204804.png" data-img="1" width="200" height="200"></img></div>
572574

573-
<a name="微信"></a>
574-
<div align="center"><img src="https://code-thinking-1253855093.file.myqcloud.com/pics/WechatIMG82.jpeg" data-img="1" width="200" height="200"></img></div>
575575

576576

577577
# 公众号
@@ -585,10 +585,3 @@
585585
<a name="公众号"></a>
586586
<div align="center"><img src="https://code-thinking-1253855093.file.myqcloud.com/pics/20211026122841.png" data-img="1" width="650" height="500"></img></div>
587587

588-
# 服务器
589-
590-
<p align="center"><strong>阿里云服务器,双11特价活动🔥🔥🔥🔥</p>
591-
<p align="center">
592-
<a href="https://www.aliyun.com/minisite/goods?taskPkg=1111ydsrwb&pkgSid=1959&recordId=962642&userCode=roof0wob" target="_blank">
593-
<img src="./pics/阿里云.png" width="1000"/>
594-
</a>

problems/0001.两数之和.md

Lines changed: 39 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<p align="center">
2-
<a href="https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ" target="_blank">
2+
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
33
<img src="https://code-thinking-1253855093.file.myqcloud.com/pics/20210924105952.png" width="1000"/>
44
</a>
55
<p align="center"><strong><a href="https://mp.weixin.qq.com/s/tqCxrMEU-ajQumL1i8im9A">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!</strong></p>
@@ -118,6 +118,18 @@ class Solution:
118118
return [records[target - val], idx] # 如果存在就返回字典记录索引和当前索引
119119
```
120120

121+
Python (v2):
122+
123+
```python
124+
class Solution:
125+
def twoSum(self, nums: List[int], target: int) -> List[int]:
126+
rec = {}
127+
for i in range(len(nums)):
128+
rest = target - nums[i]
129+
# Use get to get the index of the data, making use of one of the dictionary properties.
130+
if rec.get(rest, None) is not None: return [rec[rest], i]
131+
rec[nums[i]] = i
132+
```
121133

122134
Go:
123135

@@ -186,6 +198,24 @@ var twoSum = function (nums, target) {
186198
};
187199
```
188200

201+
TypeScript:
202+
203+
```typescript
204+
function twoSum(nums: number[], target: number): number[] {
205+
let helperMap: Map<number, number> = new Map();
206+
let index: number | undefined;
207+
let resArr: number[] = [];
208+
for (let i = 0, length = nums.length; i < length; i++) {
209+
index = helperMap.get(target - nums[i]);
210+
if (index !== undefined) {
211+
resArr = [i, index];
212+
}
213+
helperMap.set(nums[i], i);
214+
}
215+
return resArr;
216+
};
217+
```
218+
189219
php
190220

191221
```php
@@ -207,18 +237,16 @@ function twoSum(array $nums, int $target): array
207237
Swift:
208238
```swift
209239
func twoSum(_ nums: [Int], _ target: Int) -> [Int] {
210-
var res = [Int]()
211-
var dict = [Int : Int]()
212-
for i in 0 ..< nums.count {
213-
let other = target - nums[i]
214-
if dict.keys.contains(other) {
215-
res.append(i)
216-
res.append(dict[other]!)
217-
return res
240+
// 值: 下标
241+
var map = [Int: Int]()
242+
for (i, e) in nums.enumerated() {
243+
if let v = map[target - e] {
244+
return [v, i]
245+
} else {
246+
map[e] = i
218247
}
219-
dict[nums[i]] = i
220248
}
221-
return res
249+
return []
222250
}
223251
```
224252

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

Lines changed: 113 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<p align="center">
2-
<a href="https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ" target="_blank">
2+
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
33
<img src="https://code-thinking-1253855093.file.myqcloud.com/pics/20210924105952.png" width="1000"/>
44
</a>
55
<p align="center"><strong><a href="https://mp.weixin.qq.com/s/tqCxrMEU-ajQumL1i8im9A">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!</strong></p>
@@ -218,7 +218,7 @@ public:
218218
219219
一个元素可以作为中心点,两个元素也可以作为中心点。
220220
221-
那么有人同学问了,三个元素还可以做中心点呢。其实三个元素就可以由一个元素左右添加元素得到,四个元素则可以由两个元素左右添加元素得到。
221+
那么有的同学问了,三个元素还可以做中心点呢。其实三个元素就可以由一个元素左右添加元素得到,四个元素则可以由两个元素左右添加元素得到。
222222
223223
所以我们在计算的时候,要注意一个元素为中心点和两个元素为中心点的情况。
224224
@@ -260,7 +260,26 @@ public:
260260

261261
# 其他语言版本
262262

263-
## Java
263+
Java:
264+
265+
```java
266+
public int[] twoSum(int[] nums, int target) {
267+
int[] res = new int[2];
268+
if(nums == null || nums.length == 0){
269+
return res;
270+
}
271+
Map<Integer, Integer> map = new HashMap<>();
272+
for(int i = 0; i < nums.length; i++){
273+
int temp = target - nums[i];
274+
if(map.containsKey(temp)){
275+
res[1] = i;
276+
res[0] = map.get(temp);
277+
}
278+
map.put(nums[i], i);
279+
}
280+
return res;
281+
}
282+
```
264283

265284
```java
266285
// 双指针 中心扩散法
@@ -291,7 +310,7 @@ class Solution {
291310
}
292311
```
293312

294-
## Python
313+
Python
295314

296315
```python
297316
class Solution:
@@ -312,7 +331,8 @@ class Solution:
312331
return s[left:right + 1]
313332

314333
```
315-
> 双指针法:
334+
双指针:
335+
316336
```python
317337
class Solution:
318338
def longestPalindrome(self, s: str) -> str:
@@ -340,13 +360,13 @@ class Solution:
340360
return s[start:end]
341361

342362
```
343-
## Go
363+
Go:
344364

345365
```go
346366

347367
```
348368

349-
## JavaScript
369+
JavaScript
350370

351371
```js
352372
//动态规划解法
@@ -462,7 +482,93 @@ var longestPalindrome = function(s) {
462482
};
463483
```
464484

485+
C:
486+
487+
动态规划:
488+
```c
489+
//初始化dp数组,全部初始为false
490+
bool **initDP(int strLen) {
491+
bool **dp = (bool **)malloc(sizeof(bool *) * strLen);
492+
int i, j;
493+
for(i = 0; i < strLen; ++i) {
494+
dp[i] = (bool *)malloc(sizeof(bool) * strLen);
495+
for(j = 0; j < strLen; ++j)
496+
dp[i][j] = false;
497+
}
498+
return dp;
499+
}
465500

501+
char * longestPalindrome(char * s){
502+
//求出字符串长度
503+
int strLen = strlen(s);
504+
//初始化dp数组,元素初始化为false
505+
bool **dp = initDP(strLen);
506+
int maxLength = 0, left = 0, right = 0;
507+
508+
//从下到上,从左到右遍历
509+
int i, j;
510+
for(i = strLen - 1; i >= 0; --i) {
511+
for(j = i; j < strLen; ++j) {
512+
//若当前i与j所指字符一样
513+
if(s[i] == s[j]) {
514+
//若i、j指向相邻字符或同一字符,则为回文字符串
515+
if(j - i <= 1)
516+
dp[i][j] = true;
517+
//若i+1与j-1所指字符串为回文字符串,则i、j所指字符串为回文字符串
518+
else if(dp[i + 1][j - 1])
519+
dp[i][j] = true;
520+
}
521+
//若新的字符串的长度大于之前的最大长度,进行更新
522+
if(dp[i][j] && j - i + 1 > maxLength) {
523+
maxLength = j - i + 1;
524+
left = i;
525+
right = j;
526+
}
527+
}
528+
}
529+
//复制回文字符串,并返回
530+
char *ret = (char*)malloc(sizeof(char) * (maxLength + 1));
531+
memcpy(ret, s + left, maxLength);
532+
ret[maxLength] = 0;
533+
return ret;
534+
}
535+
```
536+
537+
双指针:
538+
```c
539+
int left, maxLength;
540+
void extend(char *str, int i, int j, int size) {
541+
while(i >= 0 && j < size && str[i] == str[j]) {
542+
//若当前子字符串长度大于最长的字符串长度,进行更新
543+
if(j - i + 1 > maxLength) {
544+
maxLength = j - i + 1;
545+
left = i;
546+
}
547+
//左指针左移,右指针右移。扩大搜索范围
548+
++j, --i;
549+
}
550+
}
551+
552+
char * longestPalindrome(char * s){
553+
left = right = maxLength = 0;
554+
int size = strlen(s);
555+
556+
int i;
557+
for(i = 0; i < size; ++i) {
558+
//长度为单数的子字符串
559+
extend(s, i, i, size);
560+
//长度为双数的子字符串
561+
extend(s, i, i + 1, size);
562+
}
563+
564+
//复制子字符串
565+
char *subStr = (char *)malloc(sizeof(char) * (maxLength + 1));
566+
memcpy(subStr, s + left, maxLength);
567+
subStr[maxLength] = 0;
568+
569+
return subStr;
570+
}
571+
```
466572

467573
-----------------------
468574
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>

0 commit comments

Comments
 (0)