Skip to content

Commit dd74213

Browse files
authored
Merge pull request #1 from youngyangyang04/master
同步
2 parents b1c3d5c + a27d1a4 commit dd74213

File tree

107 files changed

+3054
-422
lines changed

Some content is hidden

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

107 files changed

+3054
-422
lines changed

.DS_Store

8 KB
Binary file not shown.

README.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,11 @@
55

66
> 1. **介绍**:本项目是一套完整的刷题计划,旨在帮助大家少走弯路,循序渐进学算法,[关注作者](#关于作者)
77
> 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))的原创。引用本项目文章请注明出处,发现恶意抄袭或搬运,会动用法律武器维护自己的权益。让我们一起维护一个良好的技术创作环境!
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">
@@ -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)

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

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -462,7 +462,92 @@ var longestPalindrome = function(s) {
462462
};
463463
```
464464

465+
## C
466+
动态规划:
467+
```c
468+
//初始化dp数组,全部初始为false
469+
bool **initDP(int strLen) {
470+
bool **dp = (bool **)malloc(sizeof(bool *) * strLen);
471+
int i, j;
472+
for(i = 0; i < strLen; ++i) {
473+
dp[i] = (bool *)malloc(sizeof(bool) * strLen);
474+
for(j = 0; j < strLen; ++j)
475+
dp[i][j] = false;
476+
}
477+
return dp;
478+
}
465479

480+
char * longestPalindrome(char * s){
481+
//求出字符串长度
482+
int strLen = strlen(s);
483+
//初始化dp数组,元素初始化为false
484+
bool **dp = initDP(strLen);
485+
int maxLength = 0, left = 0, right = 0;
486+
487+
//从下到上,从左到右遍历
488+
int i, j;
489+
for(i = strLen - 1; i >= 0; --i) {
490+
for(j = i; j < strLen; ++j) {
491+
//若当前i与j所指字符一样
492+
if(s[i] == s[j]) {
493+
//若i、j指向相邻字符或同一字符,则为回文字符串
494+
if(j - i <= 1)
495+
dp[i][j] = true;
496+
//若i+1与j-1所指字符串为回文字符串,则i、j所指字符串为回文字符串
497+
else if(dp[i + 1][j - 1])
498+
dp[i][j] = true;
499+
}
500+
//若新的字符串的长度大于之前的最大长度,进行更新
501+
if(dp[i][j] && j - i + 1 > maxLength) {
502+
maxLength = j - i + 1;
503+
left = i;
504+
right = j;
505+
}
506+
}
507+
}
508+
//复制回文字符串,并返回
509+
char *ret = (char*)malloc(sizeof(char) * (maxLength + 1));
510+
memcpy(ret, s + left, maxLength);
511+
ret[maxLength] = 0;
512+
return ret;
513+
}
514+
```
515+
516+
双指针:
517+
```c
518+
int left, maxLength;
519+
void extend(char *str, int i, int j, int size) {
520+
while(i >= 0 && j < size && str[i] == str[j]) {
521+
//若当前子字符串长度大于最长的字符串长度,进行更新
522+
if(j - i + 1 > maxLength) {
523+
maxLength = j - i + 1;
524+
left = i;
525+
}
526+
//左指针左移,右指针右移。扩大搜索范围
527+
++j, --i;
528+
}
529+
}
530+
531+
char * longestPalindrome(char * s){
532+
left = right = maxLength = 0;
533+
int size = strlen(s);
534+
535+
int i;
536+
for(i = 0; i < size; ++i) {
537+
//长度为单数的子字符串
538+
extend(s, i, i, size);
539+
//长度为双数的子字符串
540+
extend(s, i, i + 1, size);
541+
}
542+
543+
//复制子字符串
544+
char *subStr = (char *)malloc(sizeof(char) * (maxLength + 1));
545+
memcpy(subStr, s + left, maxLength);
546+
subStr[maxLength] = 0;
547+
548+
return subStr;
549+
}
550+
```
466551

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

problems/0015.三数之和.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,8 +138,12 @@ public:
138138
*/
139139
if (nums[i] + nums[left] + nums[right] > 0) {
140140
right--;
141+
// 当前元素不合适了,可以去重
142+
while (left < right && nums[right] == nums[right + 1]) right--;
141143
} else if (nums[i] + nums[left] + nums[right] < 0) {
142144
left++;
145+
// 不合适,去重
146+
while (left < right && nums[left] == nums[left - 1]) left++;
143147
} else {
144148
result.push_back(vector<int>{nums[i], nums[left], nums[right]});
145149
// 去重逻辑应该放在找到一个三元组之后

problems/0018.四数之和.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,9 +91,13 @@ public:
9191
// nums[k] + nums[i] + nums[left] + nums[right] > target 会溢出
9292
if (nums[k] + nums[i] > target - (nums[left] + nums[right])) {
9393
right--;
94+
// 当前元素不合适了,可以去重
95+
while (left < right && nums[right] == nums[right + 1]) right--;
9496
// nums[k] + nums[i] + nums[left] + nums[right] < target 会溢出
9597
} else if (nums[k] + nums[i] < target - (nums[left] + nums[right])) {
9698
left++;
99+
// 不合适,去重
100+
while (left < right && nums[left] == nums[left - 1]) left++;
97101
} else {
98102
result.push_back(vector<int>{nums[k], nums[i], nums[left], nums[right]});
99103
// 去重逻辑应该放在找到一个四元组之后

problems/0020.有效的括号.md

Lines changed: 54 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ class Solution {
159159
```
160160

161161
Python:
162-
```python3
162+
```python
163163
# 方法一,仅使用栈,更省空间
164164
class Solution:
165165
def isValid(self, s: str) -> bool:
@@ -180,7 +180,7 @@ class Solution:
180180
return True if not stack else False
181181
```
182182

183-
```python3
183+
```python
184184
# 方法二,使用字典
185185
class Solution:
186186
def isValid(self, s: str) -> bool:
@@ -283,8 +283,60 @@ var isValid = function(s) {
283283
};
284284
```
285285

286+
TypeScript:
287+
288+
版本一:普通版
289+
290+
```typescript
291+
function isValid(s: string): boolean {
292+
let helperStack: string[] = [];
293+
for (let i = 0, length = s.length; i < length; i++) {
294+
let x: string = s[i];
295+
switch (x) {
296+
case '(':
297+
helperStack.push(')');
298+
break;
299+
case '[':
300+
helperStack.push(']');
301+
break;
302+
case '{':
303+
helperStack.push('}');
304+
break;
305+
default:
306+
if (helperStack.pop() !== x) return false;
307+
break;
308+
}
309+
}
310+
return helperStack.length === 0;
311+
};
312+
```
313+
314+
版本二:优化版
315+
316+
```typescript
317+
function isValid(s: string): boolean {
318+
type BracketMap = {
319+
[index: string]: string;
320+
}
321+
let helperStack: string[] = [];
322+
let bracketMap: BracketMap = {
323+
'(': ')',
324+
'[': ']',
325+
'{': '}'
326+
}
327+
for (let i of s) {
328+
if (bracketMap.hasOwnProperty(i)) {
329+
helperStack.push(bracketMap[i]);
330+
} else if (i !== helperStack.pop()) {
331+
return false;
332+
}
333+
}
334+
return helperStack.length === 0;
335+
};
336+
```
286337

287338
Swift
339+
288340
```swift
289341
func isValid(_ s: String) -> Bool {
290342
var stack = [String.Element]()

problems/0027.移除元素.md

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,37 @@ public:
106106
107107
旧文链接:[数组:就移除个元素很难么?](https://programmercarl.com/0027.移除元素.html)
108108
109+
```CPP
110+
/**
111+
* 相向双指针方法,基于元素顺序可以改变的题目描述改变了元素相对位置,确保了移动最少元素
112+
* 时间复杂度:$O(n)$
113+
* 空间复杂度:$O(1)$
114+
*/
115+
class Solution {
116+
public:
117+
int removeElement(vector<int>& nums, int val) {
118+
int leftIndex = 0;
119+
int rightIndex = nums.size() - 1;
120+
while (leftIndex <= rightIndex) {
121+
// 找左边等于val的元素
122+
while (leftIndex <= rightIndex && nums[leftIndex] != val){
123+
++leftIndex;
124+
}
125+
// 找右边不等于val的元素
126+
while (leftIndex <= rightIndex && nums[rightIndex] == val) {
127+
-- rightIndex;
128+
}
129+
// 将右边不等于val的元素覆盖左边等于val的元素
130+
if (leftIndex < rightIndex) {
131+
nums[leftIndex++] = nums[rightIndex--];
132+
}
133+
}
134+
return leftIndex; // leftIndex一定指向了最终数组末尾的下一个元素
135+
}
136+
};
137+
```
138+
139+
109140
## 相关题目推荐
110141

111142
* 26.删除排序数组中的重复项
@@ -142,7 +173,7 @@ class Solution {
142173

143174
Python:
144175

145-
```python3
176+
```python
146177
class Solution:
147178
"""双指针法
148179
时间复杂度:O(n)

problems/0028.实现strStr.md

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -929,6 +929,83 @@ var strStr = function (haystack, needle) {
929929
};
930930
```
931931

932+
TypeScript版本:
933+
934+
> 前缀表统一减一
935+
936+
```typescript
937+
function strStr(haystack: string, needle: string): number {
938+
function getNext(str: string): number[] {
939+
let next: number[] = [];
940+
let j: number = -1;
941+
next[0] = j;
942+
for (let i = 1, length = str.length; i < length; i++) {
943+
while (j >= 0 && str[i] !== str[j + 1]) {
944+
j = next[j];
945+
}
946+
if (str[i] === str[j + 1]) {
947+
j++;
948+
}
949+
next[i] = j;
950+
}
951+
return next;
952+
}
953+
if (needle.length === 0) return 0;
954+
let next: number[] = getNext(needle);
955+
let j: number = -1;
956+
for (let i = 0, length = haystack.length; i < length; i++) {
957+
while (j >= 0 && haystack[i] !== needle[j + 1]) {
958+
j = next[j];
959+
}
960+
if (haystack[i] === needle[j + 1]) {
961+
if (j === needle.length - 2) {
962+
return i - j - 1;
963+
}
964+
j++;
965+
}
966+
}
967+
return -1;
968+
};
969+
```
970+
971+
> 前缀表不减一
972+
973+
```typescript
974+
// 不减一版本
975+
function strStr(haystack: string, needle: string): number {
976+
function getNext(str: string): number[] {
977+
let next: number[] = [];
978+
let j: number = 0;
979+
next[0] = j;
980+
for (let i = 1, length = str.length; i < length; i++) {
981+
while (j > 0 && str[i] !== str[j]) {
982+
j = next[j - 1];
983+
}
984+
if (str[i] === str[j]) {
985+
j++;
986+
}
987+
next[i] = j;
988+
}
989+
return next;
990+
}
991+
if (needle.length === 0) return 0;
992+
let next: number[] = getNext(needle);
993+
let j: number = 0;
994+
for (let i = 0, length = haystack.length; i < length; i++) {
995+
while (j > 0 && haystack[i] !== needle[j]) {
996+
j = next[j - 1];
997+
}
998+
if (haystack[i] === needle[j]) {
999+
if (j === needle.length - 1) {
1000+
return i - j;
1001+
}
1002+
j++;
1003+
}
1004+
}
1005+
return -1;
1006+
}
1007+
```
1008+
9321009
Swift 版本
9331010

9341011
> 前缀表统一减一

problems/0031.下一个排列.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ public:
8181
for (int j = nums.size() - 1; j > i; j--) {
8282
if (nums[j] > nums[i]) {
8383
swap(nums[j], nums[i]);
84-
sort(nums.begin() + i + 1, nums.end());
84+
reverse(nums.begin() + i + 1, nums.end());
8585
return;
8686
}
8787
}

problems/0035.搜索插入位置.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ func searchInsert(nums []int, target int) int {
246246
```
247247

248248
### Python
249-
```python3
249+
```python
250250
class Solution:
251251
def searchInsert(self, nums: List[int], target: int) -> int:
252252
left, right = 0, len(nums) - 1

0 commit comments

Comments
 (0)