Skip to content
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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[pull] master from labuladong:master #5

Open
wants to merge 388 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
388 commits
Select commit Hold shift + click to select a range
06c5711
Merge branch 'master' into master
brucecat Mar 20, 2021
415dffe
【92. 反转链表 II】【C++】
brucecat Mar 20, 2021
0120ed1
Merge branch 'master' into master
brucecat Mar 20, 2021
ea2301d
【142. 环形链表 II】【C++】
brucecat Mar 20, 2021
ab8b9e2
Merge branch 'master' into master
brucecat Mar 20, 2021
aab3b27
【268. 丢失的数字】【 python3】
brucecat Mar 20, 2021
11d708b
Update 最长回文子串.md
enrilwang Mar 20, 2021
a3f7794
Merge branch 'master' into patch-1
brucecat Mar 20, 2021
98eab61
更正url
brucecat Mar 20, 2021
d83128b
Update 最长回文子串.md
enrilwang Mar 20, 2021
ebe8d71
更新部分参与者的url
brucecat Mar 20, 2021
7f7094d
[javascript]
Mar 20, 2021
78a32da
【javascript】
brucecat Mar 20, 2021
1522bc2
更新参与者url
brucecat Mar 21, 2021
2f8c597
Update linuxshell.md
brucecat Mar 21, 2021
c2952f5
Merge branch 'master' into master
brucecat Mar 21, 2021
6c7cfa4
【98.验证二叉搜索树】【Java】
brucecat Mar 21, 2021
637f40d
【5.最长回文子串】【python】
brucecat Mar 21, 2021
1e912c8
添加B站视频链接
labuladong Apr 19, 2021
b28bf5d
添加B站视频
labuladong Apr 19, 2021
9974091
更新链接
labuladong May 3, 2021
a6a439e
fixurl
labuladong May 10, 2021
15d5316
Update 回溯算法详解修订版.md (#653)
CharlesCCC Jul 6, 2021
c967627
Update 最优子结构.md (#633)
ndNovaDev Jul 6, 2021
e94b84c
update:更新核心算法套路
labuladong Jul 8, 2021
49ea274
feat:发布刷题辅助插件
labuladong Oct 28, 2021
4860b4f
fix:插件版本改为最新
labuladong Nov 1, 2021
2a53f9e
删除gitbook地址
labuladong Nov 9, 2021
acb26d5
update:发布刷题三件套
labuladong Nov 19, 2021
caeb33c
Update 递归详解.md
Philbenzy Nov 29, 2021
441d69d
Create .gitattributes
Dec 9, 2021
c185c1e
update
labuladong Dec 10, 2021
ab46329
update:目录结构
labuladong Feb 26, 2022
1a82ae4
update目录
labuladong Mar 25, 2022
78dd5b8
fix gitee
labuladong Mar 27, 2022
e6326d5
plugin
labuladong Apr 17, 2022
e98e3c1
feat:vscode 插件预发布
labuladong Apr 18, 2022
47a867e
Merge pull request #716 from Philbenzy/patch-1
brucecat Apr 28, 2022
373bd7b
Replace star history chart with a live one
tianzhou Jun 9, 2022
993fe82
添加全家桶介绍
labuladong Jul 12, 2022
4d64cc9
feat: 更新所有内容
labuladong Aug 24, 2022
f4cf78a
update content
labuladong Aug 24, 2022
cfa34b6
update content
labuladong Aug 24, 2022
4a60a4f
update content
labuladong Sep 1, 2022
c8fb370
update content
labuladong Sep 3, 2022
68d6053
同步网站新增功能及文章
labuladong Sep 3, 2022
93f5752
update content
labuladong Sep 4, 2022
03ccab9
update content
labuladong Sep 6, 2022
64f6fcf
update content
labuladong Sep 6, 2022
5697c4d
update content
labuladong Sep 9, 2022
625f2e7
update content
labuladong Sep 14, 2022
92885f2
update content
labuladong Sep 16, 2022
ee00da1
update content
labuladong Sep 19, 2022
c511609
update content
labuladong Sep 19, 2022
6c0ef1e
update content
labuladong Sep 29, 2022
216ecab
update content
labuladong Oct 1, 2022
e32537f
update content
labuladong Oct 4, 2022
4c76993
update content
labuladong Oct 7, 2022
2d52c5b
update content
labuladong Oct 11, 2022
76a2e0e
update content
labuladong Oct 11, 2022
24c23f4
update content
labuladong Oct 12, 2022
53f5000
update content
labuladong Oct 15, 2022
c69a6e7
update content
labuladong Oct 18, 2022
2e69ddf
update content
labuladong Oct 23, 2022
250e2b4
update content
labuladong Oct 24, 2022
a52d21f
update content
labuladong Oct 30, 2022
7b994f1
update content
labuladong Nov 3, 2022
12334c5
update content
labuladong Nov 7, 2022
c103e36
update content
labuladong Nov 12, 2022
6b096f3
update content
labuladong Nov 16, 2022
3cb9a72
update content
labuladong Nov 17, 2022
d77c58f
update content
labuladong Nov 24, 2022
d733392
update content
labuladong Nov 26, 2022
547b90e
update content
labuladong Nov 29, 2022
00d5225
update content
labuladong Dec 5, 2022
32ea0f7
update content
labuladong Dec 6, 2022
64ff1cd
update content
labuladong Dec 9, 2022
273e910
update content
labuladong Dec 11, 2022
d60c801
update content
labuladong Dec 12, 2022
0227885
update content
labuladong Dec 13, 2022
776a9e0
update content
labuladong Dec 20, 2022
8d4cce3
update content
labuladong Dec 21, 2022
e0cbfab
update content
labuladong Dec 21, 2022
7b87def
update content
labuladong Dec 29, 2022
d2f6f0b
update content
labuladong Jan 1, 2023
47b77a7
update content
labuladong Jan 5, 2023
30eceeb
update content
labuladong Jan 5, 2023
0a8e9bc
update content
labuladong Jan 8, 2023
5630dd9
update content
labuladong Jan 10, 2023
1d22b3a
update content
labuladong Jan 13, 2023
2016d5e
update content
labuladong Jan 17, 2023
0ea4770
update content
labuladong Jan 20, 2023
30b7db0
update content
labuladong Jan 22, 2023
0bbefd1
update content
labuladong Jan 23, 2023
68f32fe
update content
labuladong Jan 25, 2023
ebce75f
update content
labuladong Jan 25, 2023
eaef001
update content
labuladong Jan 30, 2023
4b8edc4
update content
labuladong Feb 1, 2023
1f8e2c8
update content
labuladong Feb 5, 2023
9a653a3
update content
labuladong Feb 6, 2023
6735cb3
update content
labuladong Feb 14, 2023
31b6d79
update content
labuladong Feb 20, 2023
22cd754
update content
labuladong Feb 24, 2023
3e4eca4
update content
labuladong Feb 25, 2023
294e1c5
update content
labuladong Feb 25, 2023
a5d82e3
update content
labuladong Feb 25, 2023
edf162e
update content
labuladong Feb 26, 2023
646b2ae
update content
labuladong Feb 26, 2023
4d6981e
update content
labuladong Feb 27, 2023
1912251
update content
labuladong Feb 28, 2023
7ab7420
update content
labuladong Feb 28, 2023
5f134ad
update content
labuladong Feb 28, 2023
8461a3d
update content
labuladong Mar 2, 2023
3e43864
update content
labuladong Mar 2, 2023
60bb5a5
update content
labuladong Mar 3, 2023
06350ad
update content
labuladong Mar 3, 2023
7008873
update content
labuladong Mar 5, 2023
b28b00f
update content
labuladong Mar 6, 2023
2a4efc4
update content
labuladong Mar 9, 2023
4acb8f3
update content
labuladong Mar 12, 2023
9ad94cc
update content
labuladong Mar 13, 2023
abbfe39
update content
labuladong Mar 13, 2023
9acc1b1
update content
labuladong Mar 15, 2023
3d91fdd
update content
labuladong Mar 16, 2023
061a93d
update content
labuladong Mar 17, 2023
f8fb50a
update content
labuladong Mar 19, 2023
83abf07
add multi lang code
labuladong Mar 19, 2023
a027e27
add contribution guide
labuladong Mar 19, 2023
6f3edb0
update content
labuladong Mar 19, 2023
bd1864e
Update bug_report.yml
labuladong Mar 19, 2023
7dd84a6
fix search-a-2d-matrix golang
labuladong Mar 19, 2023
9204860
update content
labuladong Mar 19, 2023
7270df2
update content
labuladong Mar 23, 2023
b2e2ce9
update content
labuladong Mar 26, 2023
996f468
Update solution_code.md
TroyMai Mar 26, 2023
8428201
improve tip
labuladong Mar 26, 2023
eff84d4
update content
labuladong Mar 26, 2023
97a8ad3
Update solution_code.md
TroyMai Mar 26, 2023
1868c3f
Fix qing-wa-tiao-tai-jie-wen-ti-lcof
and-star Mar 26, 2023
a31dc6d
Update solution_code.md
mrwangone Mar 26, 2023
889988f
Update bug_report.yml
labuladong Mar 27, 2023
c271437
fix bug
wwang108 Mar 27, 2023
1b89120
Update solution_code.md
mrwangone Mar 27, 2023
8996160
add fix
labuladong Mar 27, 2023
fac835e
update content
labuladong Mar 28, 2023
a72cdb7
update content
labuladong Mar 30, 2023
6d67616
update content
labuladong Apr 2, 2023
de572d6
Update solution_code.md
mrwangone Mar 29, 2023
5298aee
Fix qn8gGX
and-star Mar 30, 2023
ae286d8
update function name
aiwandianao Apr 2, 2023
e487aca
update content
labuladong Apr 4, 2023
6c845ac
update content
labuladong Apr 5, 2023
8018255
update content
labuladong Apr 5, 2023
8c874e8
update content
labuladong Apr 9, 2023
0da6679
update content
labuladong Apr 9, 2023
1d1a7a1
update solution_code.md
heshuyue Apr 11, 2023
4d601b0
update content
labuladong Apr 13, 2023
7fbcb87
update content
labuladong May 4, 2023
a234603
update content
labuladong May 4, 2023
da5c2f0
update content
labuladong May 9, 2023
7e7f9f6
update content
labuladong May 12, 2023
9945a90
update content
labuladong May 16, 2023
391169b
fix [java]perfect-squares
Apr 26, 2023
651366c
pop函数修改
Apr 24, 2023
58b1119
fix code
labuladong May 17, 2023
bd16c2e
update content
labuladong May 24, 2023
db80e20
update content
labuladong May 28, 2023
df7b1ed
update content
labuladong May 31, 2023
ff455ff
update content
labuladong Jun 2, 2023
3436f2e
update content
labuladong Jun 6, 2023
bc4507a
fix code: java 22
kkty39 Jun 4, 2023
11dc74f
fix code: java 22
kkty39 Jun 4, 2023
ca21eab
fix typo in comment
kkty39 Jun 4, 2023
af3ca8d
remove fixes in another problem
kkty39 Jun 4, 2023
fb3b506
update content
labuladong Jun 9, 2023
ad8d5be
update content
labuladong Jun 11, 2023
bbb1483
fix 83 java return type (#1414)
kkty39 Jun 13, 2023
f3b9207
[fix] [Python] g5c51o (#1401)
onmoonno Jun 13, 2023
bd55d15
update content
labuladong Jun 13, 2023
6c10132
[fix][typescript] house-robber-iii (#1406)
yyj08070631 Jun 14, 2023
6bbdd79
update content
labuladong Jun 19, 2023
7cd1ea3
update content
labuladong Jun 20, 2023
4646a3e
update content
labuladong Jun 20, 2023
f2fa753
update content
labuladong Jun 20, 2023
7e6079e
update content
labuladong Jun 26, 2023
7e237dc
update content
labuladong Jun 27, 2023
d49093f
update content
labuladong Jun 29, 2023
52456ea
update content
labuladong Jul 2, 2023
a68ab9b
update content
labuladong Jul 2, 2023
c3b258a
update content
labuladong Jul 6, 2023
ba6821c
update content
labuladong Jul 15, 2023
83984d6
update content
labuladong Jul 18, 2023
bb8eb6f
update content
labuladong Jul 20, 2023
8cafd64
update content
labuladong Jul 22, 2023
2770a85
update content
labuladong Jul 22, 2023
3414cca
update content
labuladong Jul 25, 2023
b775881
update content
labuladong Jul 27, 2023
1568745
update content
labuladong Jul 30, 2023
34b6764
update content
labuladong Aug 5, 2023
67ffc1a
update content
labuladong Aug 6, 2023
b9f49d6
update content
labuladong Aug 15, 2023
376bac2
update content
labuladong Aug 21, 2023
a28709a
update content
labuladong Aug 23, 2023
dff9cbf
update content
labuladong Aug 27, 2023
8c50622
update content
labuladong Aug 28, 2023
1869397
update content
labuladong Sep 3, 2023
92f6a25
update content
labuladong Sep 3, 2023
2b35e50
update content
labuladong Sep 6, 2023
16ec6ac
update content
labuladong Sep 20, 2023
bd7ab10
add book cover
labuladong Oct 17, 2023
64d3b35
update content
labuladong Oct 30, 2023
0b4b9dd
[fix][{python}] {find-k-pairs-with-smallest-sums} (#1500)
hyuan12 Mar 10, 2024
d17d0cd
update content
labuladong Mar 19, 2024
d2c4a77
update content
labuladong Mar 25, 2024
65a5430
update content
labuladong Mar 26, 2024
57a7ee0
update content
labuladong Mar 27, 2024
81e8bc4
update content
labuladong Mar 29, 2024
a07ee22
update content
labuladong Apr 2, 2024
d6f1c0e
update content
labuladong Apr 7, 2024
3d43935
update content
labuladong Apr 15, 2024
faccc26
update content
labuladong Apr 17, 2024
73d5818
update content
labuladong Apr 19, 2024
6da4603
update content
labuladong Apr 19, 2024
4f44711
update content
labuladong Apr 21, 2024
7181751
update content
labuladong Apr 28, 2024
20fb449
update content
labuladong May 15, 2024
994c02c
update content
labuladong May 19, 2024
caf432c
update content
labuladong Jun 9, 2024
77789ff
update content
labuladong Jun 21, 2024
d192019
update content
labuladong Jun 25, 2024
f4ec8aa
update content
labuladong Jun 25, 2024
f0a7873
update content
labuladong Jun 30, 2024
f9f11d5
update content
labuladong Jul 2, 2024
02891de
update content
labuladong Jul 8, 2024
ea12e10
update content
labuladong Jul 17, 2024
1a5d6a4
update content
labuladong Jul 17, 2024
fc4b58b
[fix][javascript] path-with-maximum-probability (#1587)
Jayden12138 Aug 28, 2024
435c10b
minimum-window-substring (#1593)
mario-huang Aug 28, 2024
9968397
diameter-of-binary-tree (#1569)
mario-huang Aug 28, 2024
291e8cd
fix(javascript): merge-k-sorted-lists (#1575)
Jayden12138 Aug 28, 2024
ac2aa71
[fix][golang] implement-stack-using-queues (#1601)
mario-huang Aug 28, 2024
772dc16
Update issue templates
labuladong Aug 29, 2024
a330b7b
Update issue templates
labuladong Aug 29, 2024
27e6347
Update issue templates
labuladong Aug 29, 2024
d719b2c
Update issue templates
labuladong Aug 29, 2024
ae699d2
Update issue templates
labuladong Aug 29, 2024
dff3ca2
Update issue templates
labuladong Aug 29, 2024
44978cd
Update issue templates
labuladong Aug 31, 2024
8dc6ef5
Update README.md
labuladong Sep 21, 2024
b07478f
order issue template
labuladong Sep 22, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*.md linguist-detectable=true
*.md linguist-documentation=false
21 changes: 21 additions & 0 deletions .github/ISSUE_TEMPLATE/01-algo-website-bug.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
---
name: Website bug
about: Report bug on website `labuladong.online`
title: ''
labels: algo-websie-bug
assignees: labuladong

---

**Version:**
What's the extension version are you using?

**Describe the bug**
A clear and concise description of what the bug is.

**Screenshots**
If applicable, add screenshots to help explain your problem.

**Platform**
Mobile phone or PC?
What kind of web browser? (chrome/edge/...)
10 changes: 10 additions & 0 deletions .github/ISSUE_TEMPLATE/02-algo-visualize-bug.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
name: Algo-visualize bug
about: Report bug for algo-visualize tool in website/plugins
title: ''
labels: algo-visualize-bug
assignees: labuladong

---


17 changes: 17 additions & 0 deletions .github/ISSUE_TEMPLATE/03-chrome-extension-bug.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
---
name: Chrome extension bug
about: Report bug on Chrome extension
title: ''
labels: algo-website, chrome-extension-bug
assignees: labuladong

---

**Describe the bug**
A clear and concise description of what the bug is.

**Screenshots**
If applicable, add screenshots to help explain your problem.

**Platform**
What kind of web browser are you using? (chrome/edge/...)
17 changes: 17 additions & 0 deletions .github/ISSUE_TEMPLATE/04-vscode-extension-bug.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
---
name: VSCode extension bug
about: Report bug on vscode extension
title: ''
labels: vscode-extension-bug
assignees: labuladong

---

**Version:**
What's the extension version are you using?

**Describe the bug**
A clear and concise description of what the bug is.

**Screenshots**
If applicable, add screenshots to help explain your problem.
17 changes: 17 additions & 0 deletions .github/ISSUE_TEMPLATE/05-jetbrain-plugin-bug.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
---
name: JetBrain plugin bug
about: Report bug on JetBrain plugin
title: ''
labels: jb-plugin-bug
assignees: labuladong

---

**Version:**
What's the plugin version are you using?

**Describe the bug**
A clear and concise description of what the bug is.

**Screenshots**
If applicable, add screenshots to help explain your problem.
12 changes: 12 additions & 0 deletions .github/ISSUE_TEMPLATE/06-suggestion.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
---
name: Suggestion
about: Suggest an idea/improvement for this project
title: ''
labels: feature-request
assignees: labuladong

---

Do you have any suggestions?

Is there anything that you feel inconvenient to use?
21 changes: 0 additions & 21 deletions .github/ISSUE_TEMPLATE/bug_report.md

This file was deleted.

10 changes: 0 additions & 10 deletions .github/ISSUE_TEMPLATE/others.md

This file was deleted.

28 changes: 0 additions & 28 deletions .github/ISSUE_TEMPLATE/translate.md

This file was deleted.

Empty file.
415 changes: 288 additions & 127 deletions README.md

Large diffs are not rendered by default.

Binary file added pictures/cover.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file removed pictures/labuladong.jpg
Binary file not shown.
Binary file added pictures/plugin/chrome.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added pictures/plugin/chrome.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added pictures/plugin/jetbrain.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added pictures/plugin/jetbrain.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added pictures/plugin/vscode.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added pictures/plugin/vscode.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added pictures/plugin/全家桶.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified pictures/qrcode.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file removed pictures/result.jpg
Binary file not shown.
Binary file modified pictures/souyisou.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added pictures/souyisou2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file removed pictures/table_qr2.jpg
Binary file not shown.
Binary file added pictures/全家桶.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
86 changes: 86 additions & 0 deletions 动态规划系列/LCS.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
# 详解最长公共子序列问题,秒杀三道动态规划题目

<p align='center'>
<a href="https://github.com/labuladong/fucking-algorithm" target="view_window"><img alt="GitHub" src="https://img.shields.io/github/stars/labuladong/fucking-algorithm?label=Stars&style=flat-square&logo=GitHub"></a>
<a href="https://labuladong.online/algo/" target="_blank"><img class="my_header_icon" src="https://img.shields.io/static/v1?label=精品课程&message=查看&color=pink&style=flat"></a>
<a href="https://www.zhihu.com/people/labuladong"><img src="https://img.shields.io/badge/%E7%9F%A5%E4%B9%[email protected]?style=flat-square&logo=Zhihu"></a>
<a href="https://space.bilibili.com/14089380"><img src="https://img.shields.io/badge/B站[email protected]?style=flat-square&logo=Bilibili"></a>
</p>

![](https://labuladong.online/algo/images/souyisou1.png)

**通知:[新版网站会员](https://labuladong.online/algo/intro/site-vip/) 即将涨价;已支持老用户续费~另外,建议你在我的 [网站](https://labuladong.online/algo/) 学习文章,体验更好。**



读完本文,你不仅学会了算法套路,还可以顺便解决如下题目:

| LeetCode | 力扣 | 难度 |
| :----: | :----: | :----: |
| [1143. Longest Common Subsequence](https://leetcode.com/problems/longest-common-subsequence/) | [1143. 最长公共子序列](https://leetcode.cn/problems/longest-common-subsequence/) | 🟠
| [583. Delete Operation for Two Strings](https://leetcode.com/problems/delete-operation-for-two-strings/) | [583. 两个字符串的删除操作](https://leetcode.cn/problems/delete-operation-for-two-strings/) | 🟠
| [712. Minimum ASCII Delete Sum for Two Strings](https://leetcode.com/problems/minimum-ascii-delete-sum-for-two-strings/) | [712. 两个字符串的最小ASCII删除和](https://leetcode.cn/problems/minimum-ascii-delete-sum-for-two-strings/) | 🟠
| - | [剑指 Offer II 095. 最长公共子序列](https://leetcode.cn/problems/qJnOS7/) | 🟠

**-----------**

不知道大家做算法题有什么感觉,**我总结出来做算法题的技巧就是,把大的问题细化到一个点,先研究在这个小的点上如何解决问题,然后再通过递归/迭代的方式扩展到整个问题**。

比如说我们前文 [手把手带你刷二叉树第三期](https://labuladong.online/algo/data-structure/binary-tree-part3/),解决二叉树的题目,我们就会把整个问题细化到某一个节点上,想象自己站在某个节点上,需要做什么,然后套二叉树递归框架就行了。

动态规划系列问题也是一样,尤其是子序列相关的问题。**本文从「最长公共子序列问题」展开,总结三道子序列问题**,解这道题仔细讲讲这种子序列问题的套路,你就能感受到这种思维方式了。

### 最长公共子序列

计算最长公共子序列(Longest Common Subsequence,简称 LCS)是一道经典的动态规划题目,力扣第 1143 题「最长公共子序列」就是这个问题:

给你输入两个字符串 `s1` 和 `s2`,请你找出他们俩的最长公共子序列,返回这个子序列的长度。函数签名如下:

<!-- muliti_language -->
```java
int longestCommonSubsequence(String s1, String s2);
```

比如说输入 `s1 = "zabcde", s2 = "acez"`,它俩的最长公共子序列是 `lcs = "ace"`,长度为 3,所以算法返回 3。

如果没有做过这道题,一个最简单的暴力算法就是,把 `s1` 和 `s2` 的所有子序列都穷举出来,然后看看有没有公共的,然后在所有公共子序列里面再寻找一个长度最大的。

显然,这种思路的复杂度非常高,你要穷举出所有子序列,这个复杂度就是指数级的,肯定不实际。

正确的思路是不要考虑整个字符串,而是细化到 `s1` 和 `s2` 的每个字符。前文 [子序列解题模板](https://labuladong.online/algo/dynamic-programming/subsequence-problem/) 中总结的一个规律:



<hr>
<details class="hint-container details">
<summary><strong>引用本文的文章</strong></summary>

- [动态规划之子序列问题解题模板](https://labuladong.online/algo/dynamic-programming/subsequence-problem/)
- [经典动态规划:编辑距离](https://labuladong.online/algo/dynamic-programming/edit-distance/)

</details><hr>




<hr>
<details class="hint-container details">
<summary><strong>引用本文的题目</strong></summary>

<strong>安装 [我的 Chrome 刷题插件](https://labuladong.online/algo/intro/chrome/) 点开下列题目可直接查看解题思路:</strong>

| LeetCode | 力扣 |
| :----: | :----: |
| [97. Interleaving String](https://leetcode.com/problems/interleaving-string/?show=1) | [97. 交错字符串](https://leetcode.cn/problems/interleaving-string/?show=1) |
| - | [剑指 Offer II 095. 最长公共子序列](https://leetcode.cn/problems/qJnOS7/?show=1) |

</details>
<hr>



**_____________**

本文为会员内容,请扫码关注公众号或 [点这里](https://labuladong.online/algo/dynamic-programming/longest-common-subsequence/) 查看:

![](https://labuladong.online/algo/images/qrcode.jpg)
Loading