Movatterモバイル変換


[0]ホーム

URL:


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

[pull] master from youngyangyang04:master#537

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Open
pull wants to merge37 commits intoAlgorithmAndLeetCode:master
base:master
Choose a base branch
Loading
fromyoungyangyang04:master
Open
Show file tree
Hide file tree
Changes fromall commits
Commits
Show all changes
37 commits
Select commitHold shift + click to select a range
7f5ce0a
Update 0216.组合总和III.md
lewisengJan 25, 2025
3b2373d
Update 0001.两数之和.md
buptz2bFeb 4, 2025
4f07f35
fix: 修改 栈与队列理论基础.md 错别字
Asa0oo0o0oFeb 10, 2025
3176a0c
fix: 修改 README.md 错别字
Asa0oo0o0oFeb 10, 2025
2c218d7
修改0209.长度最小的子数组.md笔误
Feb 13, 2025
da82173
更新:添加kamacoder/0058区间和的Go版本
HuangLM03Feb 16, 2025
f802fc7
添加kamacoder/0047.参会dijkstra朴素的Go版本
HuangLM03Feb 16, 2025
d6e6804
Update 0454.四数相加II.md
zenwangzyMar 4, 2025
1378685
0126.骑士的攻击astar.md添加Java版本
curforeverMar 10, 2025
ca2cffa
更新readme
youngyangyang04Mar 14, 2025
0881fb5
更新图
youngyangyang04Mar 14, 2025
f3c481e
更新文章头部
youngyangyang04Mar 14, 2025
18ba181
修改表达
youngyangyang04Mar 16, 2025
73a3cb5
更新描述
youngyangyang04Mar 17, 2025
91e7dab
更新表达
youngyangyang04Mar 17, 2025
86a0208
替换图片链接
youngyangyang04Mar 17, 2025
b489cb6
Merge pull request #2896 from HuangLM03/update-kamacoder/0058区间和
youngyangyang04Apr 9, 2025
25beacf
Merge branch 'youngyangyang04:master' into master
Asa0oo0o0oApr 21, 2025
ce6e658
更新图片链接
May 19, 2025
2b0ca46
Merge pull request #2879 from lewiseng/patch-1
youngyangyang04May 19, 2025
de5b186
修改一个错别字
chenzhiwMay 23, 2025
c1f4e6f
Merge pull request #2882 from buptz2b/master
youngyangyang04Jun 15, 2025
f465f89
Merge pull request #2887 from Asa0oo0o0o/master
youngyangyang04Jun 15, 2025
4d077c4
Merge pull request #2942 from chenzhiw/patch-1
youngyangyang04Jun 15, 2025
e957225
Merge pull request #2894 from wang1xiang/master
youngyangyang04Jun 15, 2025
226f6c0
fix(docs): 修复《如何在Github上提交PR》中的文字错误
hiiirokoJun 28, 2025
7aa973b
Update
youngyangyang04Jul 6, 2025
0afc3f7
修复0494.目标和题目Python二维DP解法中的错误
zkwsJul 16, 2025
c848414
Merge pull request #2959 from zkws/master
youngyangyang04Aug 13, 2025
a6caded
Merge pull request #2950 from hiiiroko/join-doc-fix
youngyangyang04Aug 13, 2025
784437b
Merge pull request #2907 from zenwangzy/master
youngyangyang04Aug 13, 2025
3845b07
Merge pull request #2915 from curforever/master
youngyangyang04Aug 13, 2025
627c0b9
Merge pull request #2898 from HuangLM03/update-kamacoder/0047参会dijkst…
youngyangyang04Sep 12, 2025
95cb535
update readme
youngyangyang04Sep 30, 2025
a6c33f8
Update
youngyangyang04Sep 30, 2025
32736f2
update readme
youngyangyang04Sep 30, 2025
60b1ce6
Update readme
youngyangyang04Sep 30, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
302 changes: 137 additions & 165 deletionsREADME.md
View file
Open in desktop

Large diffs are not rendered by default.

29 changes: 11 additions & 18 deletionsproblems/0001.两数之和.md
100644 → 100755
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
<p align="center">
<a href="https://www.programmercarl.com/xunlian/xunlianying.html" target="_blank">
<img src="../pics/训练营.png" width="1000"/>
</a>
<p align="center"><strong><a href="./qita/join.md">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!</strong></p>
* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html)
* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html)
* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html)


# 1. 两数之和
Expand DownExpand Up@@ -85,10 +83,10 @@ map目的用来存放我们访问过的元素,因为遍历数组的时候,

过程如下:

![过程一](https://code-thinking-1253855093.file.myqcloud.com/pics/20220711202638.png)
![过程一](https://file1.kamacoder.com/i/algo/20220711202638.png)


![过程二](https://code-thinking-1253855093.file.myqcloud.com/pics/20230220223536.png)
![过程二](https://file1.kamacoder.com/i/algo/20230220223536.png)

C++代码:

Expand DownExpand Up@@ -287,17 +285,16 @@ func twoSum(nums []int, target int) []int {
```

```go
// 使用map方式解题,降低时间复杂度
func twoSum(nums []int, target int) []int {
m := make(map[int]int)
for index, val := range nums {
if preIndex, ok := m[target-val]; ok {
return []int{preIndex, index}
} else {
m[val] = index
for i, num := range nums {
complement := target - num
if index, found := m[complement]; found {
return []int{index, i}
}
m[num] = i
}
return[]int{}
returnnil // 返回空数组 nil 代替空切片
}
```

Expand DownExpand Up@@ -557,7 +554,3 @@ int* twoSum(int* nums, int numsSize, int target, int* returnSize){
}
```

<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>
16 changes: 5 additions & 11 deletionsproblems/0005.最长回文子串.md
100644 → 100755
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
<p align="center">
<a href="https://www.programmercarl.com/xunlian/xunlianying.html" target="_blank">
<img src="../pics/训练营.png" width="1000"/>
</a>
<p align="center"><strong><a href="./qita/join.md">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!</strong></p>
* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html)
* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html)
* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html)



Expand DownExpand Up@@ -108,7 +106,7 @@ dp[i][j]可以初始化为true么? 当然不行,怎能刚开始就全都匹

dp[i + 1][j - 1] 在 dp[i][j]的左下角,如图:

![647.回文子串](https://code-thinking-1253855093.file.myqcloud.com/pics/20210121171032473.jpg)
![647.回文子串](https://file1.kamacoder.com/i/algo/20210121171032473.jpg)

如果这矩阵是从上到下,从左到右遍历,那么会用到没有计算过的dp[i + 1][j - 1],也就是根据不确定是不是回文的区间[i+1,j-1],来判断了[i,j]是不是回文,那结果一定是不对的。

Expand DownExpand Up@@ -142,7 +140,7 @@ for (int i = s.size() - 1; i >= 0; i--) { // 注意遍历顺序

举例,输入:"aaa",dp[i][j]状态如下:

![647.回文子串1](https://code-thinking-1253855093.file.myqcloud.com/pics/20210121171059951.jpg)
![647.回文子串1](https://file1.kamacoder.com/i/algo/20210121171059951.jpg)

**注意因为dp[i][j]的定义,所以j一定是大于等于i的,那么在填充dp[i][j]的时候一定是只填充右上半部分**。

Expand DownExpand Up@@ -731,8 +729,4 @@ public class Solution {
```


<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>

14 changes: 4 additions & 10 deletionsproblems/0015.三数之和.md
100644 → 100755
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
<p align="center">
<a href="https://www.programmercarl.com/xunlian/xunlianying.html" target="_blank">
<img src="../pics/训练营.png" width="1000"/>
</a>
<p align="center"><strong><a href="./qita/join.md">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!</strong></p>
* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html)
* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html)
* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html)



Expand DownExpand Up@@ -102,7 +100,7 @@ public:

动画效果如下:

![15.三数之和](https://code-thinking.cdn.bcebos.com/gifs/15.%E4%B8%89%E6%95%B0%E4%B9%8B%E5%92%8C.gif)
![15.三数之和](https://file1.kamacoder.com/i/algo/15.%E4%B8%89%E6%95%B0%E4%B9%8B%E5%92%8C.gif)

拿这个nums数组来举例,首先将数组排序,然后有一层for循环,i从下标0的地方开始,同时定一个下标left 定义在i+1的位置上,定义下标right 在数组结尾的位置上。

Expand DownExpand Up@@ -980,7 +978,3 @@ object Solution {
}
```

<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>
16 changes: 5 additions & 11 deletionsproblems/0017.电话号码的字母组合.md
100644 → 100755
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
<p align="center">
<a href="https://www.programmercarl.com/xunlian/xunlianying.html" target="_blank">
<img src="../pics/训练营.png" width="1000"/>
</a>
<p align="center"><strong><a href="./qita/join.md">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!</strong></p>
* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html)
* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html)
* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html)


# 17.电话号码的字母组合
Expand All@@ -13,7 +11,7 @@

给出数字到字母的映射如下(与电话按键相同)。注意 1 不对应任何字母。

![17.电话号码的字母组合](https://code-thinking-1253855093.file.myqcloud.com/pics/2020102916424043.png)
![17.电话号码的字母组合](https://file1.kamacoder.com/i/algo/2020102916424043.png)

示例:
* 输入:"23"
Expand DownExpand Up@@ -66,7 +64,7 @@ const string letterMap[10] = {

例如:输入:"23",抽象为树形结构,如图所示:

![17. 电话号码的字母组合](https://code-thinking-1253855093.file.myqcloud.com/pics/20201123200304469.png)
![17. 电话号码的字母组合](https://file1.kamacoder.com/i/algo/20201123200304469.png)

图中可以看出遍历的深度,就是输入"23"的长度,而叶子节点就是我们要收集的结果,输出["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"]。

Expand DownExpand Up@@ -766,7 +764,3 @@ public class Solution
```


<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>
12 changes: 3 additions & 9 deletionsproblems/0018.四数之和.md
100644 → 100755
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
<p align="center">
<a href="https://www.programmercarl.com/xunlian/xunlianying.html" target="_blank">
<img src="../pics/训练营.png" width="1000"/>
</a>
<p align="center"><strong><a href="./qita/join.md">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!</strong></p>
* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html)
* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html)
* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html)


> 一样的道理,能解决四数之和
Expand DownExpand Up@@ -798,8 +796,4 @@ def four_sum(nums, target)
end
```

<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>

22 changes: 8 additions & 14 deletionsproblems/0019.删除链表的倒数第N个节点.md
100644 → 100755
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
<p align="center">
<a href="https://www.programmercarl.com/xunlian/xunlianying.html" target="_blank">
<img src="../pics/训练营.png" width="1000"/>
</a>
<p align="center"><strong><a href="./qita/join.md">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!</strong></p>
* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html)
* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html)
* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html)



Expand All@@ -18,7 +16,7 @@
示例 1:


![19.删除链表的倒数第N个节点](https://code-thinking-1253855093.file.myqcloud.com/pics/20210510085957392.png)
![19.删除链表的倒数第N个节点](https://file1.kamacoder.com/i/algo/20210510085957392.png)

输入:head = [1,2,3,4,5], n = 2
输出:[1,2,3,5]
Expand DownExpand Up@@ -51,16 +49,16 @@

* 定义fast指针和slow指针,初始值为虚拟头结点,如图:

<img src='https://code-thinking.cdn.bcebos.com/pics/19.%E5%88%A0%E9%99%A4%E9%93%BE%E8%A1%A8%E7%9A%84%E5%80%92%E6%95%B0%E7%AC%ACN%E4%B8%AA%E8%8A%82%E7%82%B9.png' width=600> </img></div>
<img src='https://file1.kamacoder.com/i/algo/19.%E5%88%A0%E9%99%A4%E9%93%BE%E8%A1%A8%E7%9A%84%E5%80%92%E6%95%B0%E7%AC%ACN%E4%B8%AA%E8%8A%82%E7%82%B9.png' width=600> </img></div>

* fast首先走n + 1步 ,为什么是n+1呢,因为只有这样同时移动的时候slow才能指向删除节点的上一个节点(方便做删除操作),如图:
<img src='https://code-thinking.cdn.bcebos.com/pics/19.%E5%88%A0%E9%99%A4%E9%93%BE%E8%A1%A8%E7%9A%84%E5%80%92%E6%95%B0%E7%AC%ACN%E4%B8%AA%E8%8A%82%E7%82%B91.png' width=600> </img></div>
<img src='https://file1.kamacoder.com/i/algo/19.%E5%88%A0%E9%99%A4%E9%93%BE%E8%A1%A8%E7%9A%84%E5%80%92%E6%95%B0%E7%AC%ACN%E4%B8%AA%E8%8A%82%E7%82%B91.png' width=600> </img></div>

* fast和slow同时移动,直到fast指向末尾,如题:
<img src='https://code-thinking.cdn.bcebos.com/pics/19.%E5%88%A0%E9%99%A4%E9%93%BE%E8%A1%A8%E7%9A%84%E5%80%92%E6%95%B0%E7%AC%ACN%E4%B8%AA%E8%8A%82%E7%82%B92.png' width=600> </img></div>
<img src='https://file1.kamacoder.com/i/algo/19.%E5%88%A0%E9%99%A4%E9%93%BE%E8%A1%A8%E7%9A%84%E5%80%92%E6%95%B0%E7%AC%ACN%E4%B8%AA%E8%8A%82%E7%82%B92.png' width=600> </img></div>
//图片中有错别词:应该将“只到”改为“直到”
* 删除slow指向的下一个节点,如图:
<img src='https://code-thinking.cdn.bcebos.com/pics/19.%E5%88%A0%E9%99%A4%E9%93%BE%E8%A1%A8%E7%9A%84%E5%80%92%E6%95%B0%E7%AC%ACN%E4%B8%AA%E8%8A%82%E7%82%B93.png' width=600> </img></div>
<img src='https://file1.kamacoder.com/i/algo/19.%E5%88%A0%E9%99%A4%E9%93%BE%E8%A1%A8%E7%9A%84%E5%80%92%E6%95%B0%E7%AC%ACN%E4%B8%AA%E8%8A%82%E7%82%B93.png' width=600> </img></div>

此时不难写出如下C++代码:

Expand DownExpand Up@@ -479,7 +477,3 @@ public class Solution {
}
}
```
<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>
20 changes: 7 additions & 13 deletionsproblems/0020.有效的括号.md
100644 → 100755
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
<p align="center">
<a href="https://www.programmercarl.com/xunlian/xunlianying.html" target="_blank">
<img src="../pics/训练营.png" width="1000"/>
</a>
<p align="center"><strong><a href="./qita/join.md">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!</strong></p>
* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html)
* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html)
* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html)



Expand DownExpand Up@@ -83,21 +81,21 @@ cd a/b/c/../../


1. 第一种情况,字符串里左方向的括号多余了 ,所以不匹配。
![括号匹配1](https://code-thinking-1253855093.file.myqcloud.com/pics/2020080915505387.png)
![括号匹配1](https://file1.kamacoder.com/i/algo/2020080915505387.png)

2. 第二种情况,括号没有多余,但是 括号的类型没有匹配上。
![括号匹配2](https://code-thinking-1253855093.file.myqcloud.com/pics/20200809155107397.png)
![括号匹配2](https://file1.kamacoder.com/i/algo/20200809155107397.png)

3. 第三种情况,字符串里右方向的括号多余了,所以不匹配。
![括号匹配3](https://code-thinking-1253855093.file.myqcloud.com/pics/20200809155115779.png)
![括号匹配3](https://file1.kamacoder.com/i/algo/20200809155115779.png)



我们的代码只要覆盖了这三种不匹配的情况,就不会出问题,可以看出 动手之前分析好题目的重要性。

动画如下:

![20.有效括号](https://code-thinking.cdn.bcebos.com/gifs/20.有效括号.gif)
![20.有效括号](https://file1.kamacoder.com/i/algo/20.有效括号.gif)


第一种情况:已经遍历完了字符串,但是栈不为空,说明有相应的左括号没有右括号来匹配,所以return false
Expand DownExpand Up@@ -574,8 +572,4 @@ impl Solution {
}
```

<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>

22 changes: 8 additions & 14 deletionsproblems/0024.两两交换链表中的节点.md
100644 → 100755
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
<p align="center">
<a href="https://www.programmercarl.com/xunlian/xunlianying.html" target="_blank">
<img src="../pics/训练营.png" width="1000"/>
</a>
<p align="center"><strong><a href="./qita/join.md">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!</strong></p>
* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html)
* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html)
* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html)


# 24. 两两交换链表中的节点
Expand All@@ -14,7 +12,7 @@
你不能只是单纯的改变节点内部的值,而是需要实际的进行节点交换。


<img src='https://code-thinking.cdn.bcebos.com/pics/24.%E4%B8%A4%E4%B8%A4%E4%BA%A4%E6%8D%A2%E9%93%BE%E8%A1%A8%E4%B8%AD%E7%9A%84%E8%8A%82%E7%82%B9-%E9%A2%98%E6%84%8F.jpg' width=600 alt='24.两两交换链表中的节点-题意'> </img></div>
<img src='https://file1.kamacoder.com/i/algo/24.%E4%B8%A4%E4%B8%A4%E4%BA%A4%E6%8D%A2%E9%93%BE%E8%A1%A8%E4%B8%AD%E7%9A%84%E8%8A%82%E7%82%B9-%E9%A2%98%E6%84%8F.jpg' width=600 alt='24.两两交换链表中的节点-题意'> </img></div>

## 算法公开课

Expand All@@ -33,16 +31,16 @@

初始时,cur指向虚拟头结点,然后进行如下三步:

![24.两两交换链表中的节点1](https://code-thinking.cdn.bcebos.com/pics/24.%E4%B8%A4%E4%B8%A4%E4%BA%A4%E6%8D%A2%E9%93%BE%E8%A1%A8%E4%B8%AD%E7%9A%84%E8%8A%82%E7%82%B91.png)
![24.两两交换链表中的节点1](https://file1.kamacoder.com/i/algo/24.%E4%B8%A4%E4%B8%A4%E4%BA%A4%E6%8D%A2%E9%93%BE%E8%A1%A8%E4%B8%AD%E7%9A%84%E8%8A%82%E7%82%B91.png)

操作之后,链表如下:

![24.两两交换链表中的节点2](https://code-thinking.cdn.bcebos.com/pics/24.%E4%B8%A4%E4%B8%A4%E4%BA%A4%E6%8D%A2%E9%93%BE%E8%A1%A8%E4%B8%AD%E7%9A%84%E8%8A%82%E7%82%B92.png)
![24.两两交换链表中的节点2](https://file1.kamacoder.com/i/algo/24.%E4%B8%A4%E4%B8%A4%E4%BA%A4%E6%8D%A2%E9%93%BE%E8%A1%A8%E4%B8%AD%E7%9A%84%E8%8A%82%E7%82%B92.png)

看这个可能就更直观一些了:


![24.两两交换链表中的节点3](https://code-thinking.cdn.bcebos.com/pics/24.%E4%B8%A4%E4%B8%A4%E4%BA%A4%E6%8D%A2%E9%93%BE%E8%A1%A8%E4%B8%AD%E7%9A%84%E8%8A%82%E7%82%B93.png)
![24.两两交换链表中的节点3](https://file1.kamacoder.com/i/algo/24.%E4%B8%A4%E4%B8%A4%E4%BA%A4%E6%8D%A2%E9%93%BE%E8%A1%A8%E4%B8%AD%E7%9A%84%E8%8A%82%E7%82%B93.png)

对应的C++代码实现如下: (注释中详细和如上图中的三步做对应)

Expand DownExpand Up@@ -83,7 +81,7 @@ public:

心想应该没有更好的方法了吧,也就 $O(n)$ 的时间复杂度,重复提交几次,这样了:

![24.两两交换链表中的节点](https://code-thinking.cdn.bcebos.com/pics/24.%E4%B8%A4%E4%B8%A4%E4%BA%A4%E6%8D%A2%E9%93%BE%E8%A1%A8%E4%B8%AD%E7%9A%84%E8%8A%82%E7%82%B9.png)
![24.两两交换链表中的节点](https://file1.kamacoder.com/i/algo/24.%E4%B8%A4%E4%B8%A4%E4%BA%A4%E6%8D%A2%E9%93%BE%E8%A1%A8%E4%B8%AD%E7%9A%84%E8%8A%82%E7%82%B9.png)

力扣上的统计如果两份代码是 100ms 和 300ms的耗时,其实是需要注意的。

Expand DownExpand Up@@ -527,7 +525,3 @@ public ListNode SwapPairs(ListNode head)
}
```

<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>
16 changes: 5 additions & 11 deletionsproblems/0027.移除元素.md
100644 → 100755
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
<p align="center">
<a href="https://www.programmercarl.com/xunlian/xunlianying.html" target="_blank">
<img src="../pics/训练营.png" width="1000"/>
</a>
<p align="center"><strong><a href="./qita/join.md">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!</strong></p>
* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html)
* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html)
* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html)


# 27. 移除元素
Expand DownExpand Up@@ -45,7 +43,7 @@

删除过程如下:

![27.移除元素-暴力解法](https://code-thinking.cdn.bcebos.com/gifs/27.%E7%A7%BB%E9%99%A4%E5%85%83%E7%B4%A0-%E6%9A%B4%E5%8A%9B%E8%A7%A3%E6%B3%95.gif)
![27.移除元素-暴力解法](https://file1.kamacoder.com/i/algo/27.%E7%A7%BB%E9%99%A4%E5%85%83%E7%B4%A0-%E6%9A%B4%E5%8A%9B%E8%A7%A3%E6%B3%95.gif)

很明显暴力解法的时间复杂度是O(n^2),这道题目暴力解法在leetcode上是可以过的。

Expand DownExpand Up@@ -89,7 +87,7 @@ public:

删除过程如下:

![27.移除元素-双指针法](https://code-thinking.cdn.bcebos.com/gifs/27.%E7%A7%BB%E9%99%A4%E5%85%83%E7%B4%A0-%E5%8F%8C%E6%8C%87%E9%92%88%E6%B3%95.gif)
![27.移除元素-双指针法](https://file1.kamacoder.com/i/algo/27.%E7%A7%BB%E9%99%A4%E5%85%83%E7%B4%A0-%E5%8F%8C%E6%8C%87%E9%92%88%E6%B3%95.gif)

很多同学不了解

Expand DownExpand Up@@ -519,7 +517,3 @@ int removeElement(List<int> nums, int val) {

```

<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>
Loading

[8]ページ先頭

©2009-2025 Movatter.jp