Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 3feae28

Browse files
Merge branch 'youngyangyang04:master' into remote
2 parents 4154d3d + de87d55 commit 3feae28

File tree

236 files changed

+7976
-1668
lines changed

Some content is hidden

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

236 files changed

+7976
-1668
lines changed

‎README.md

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11

2-
32
👉 推荐 [在线阅读](http://programmercarl.com/) (Github在国内访问经常不稳定)
43
👉 推荐 [Gitee同步](https://gitee.com/programmercarl/leetcode-master)
54

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

8989
## 前序
9090

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

9493

9594
* 编程语言
@@ -123,7 +122,7 @@
123122

124123
* 算法性能分析
125124
* [关于时间复杂度,你不知道的都在这里!](./problems/前序/关于时间复杂度,你不知道的都在这里!.md)
126-
* [$O(n)$的算法居然超时了,此时的n究竟是多大?](./problems/前序/On的算法居然超时了,此时的n究竟是多大?.md)
125+
* [O(n)的算法居然超时了,此时的n究竟是多大?](./problems/前序/On的算法居然超时了,此时的n究竟是多大?.md)
127126
* [通过一道面试题目,讲一讲递归算法的时间复杂度!](./problems/前序/通过一道面试题目,讲一讲递归算法的时间复杂度!.md)
128127
* [本周小结!(算法性能分析系列一)](./problems/周总结/20201210复杂度分析周末总结.md)
129128
* [关于空间复杂度,可能有几个疑问?](./problems/前序/关于空间复杂度,可能有几个疑问?.md)
@@ -263,7 +262,7 @@
263262
3. [二叉树:听说递归能做的,栈也能做!](./problems/二叉树的迭代遍历.md)
264263
4. [二叉树:前中后序迭代方式的写法就不能统一一下么?](./problems/二叉树的统一迭代法.md)
265264
5. [二叉树:层序遍历登场!](./problems/0102.二叉树的层序遍历.md)
266-
6. [二叉树:你真的会翻转二叉树么?](./problems/0226.翻转二叉树.md)
265+
6. [二叉树:你真的会翻转二叉树么?](./problems/0226.翻转二叉树.md)
267266
7. [本周小结!(二叉树)](./problems/周总结/20200927二叉树周末总结.md)
268267
8. [二叉树:我对称么?](./problems/0101.对称二叉树.md)
269268
9. [二叉树:看看这些树的最大深度](./problems/0104.二叉树的最大深度.md)

‎problems/0001.两数之和.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

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

Lines changed: 116 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838

3939
两层for循环,遍历区间起始位置和终止位置,然后判断这个区间是不是回文。
4040

41-
时间复杂度:$O(n^3)$
41+
时间复杂度:O(n^3)
4242

4343
## 动态规划
4444

@@ -205,8 +205,8 @@ public:
205205

206206
```
207207
208-
* 时间复杂度:$O(n^2)$
209-
* 空间复杂度:$O(n^2)$
208+
* 时间复杂度:O(n^2)
209+
* 空间复杂度:O(n^2)
210210
211211
## 双指针
212212
@@ -253,14 +253,33 @@ public:
253253
254254
```
255255

256-
* 时间复杂度:$O(n^2)$
257-
* 空间复杂度:$O(1)$
256+
* 时间复杂度:O(n^2)
257+
* 空间复杂度:O(1)
258258

259259

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)

AltStyle によって変換されたページ (->オリジナル) /