Skip to content

Commit 7b36381

Browse files
zhouboyi1998zhouboyi
authored andcommitted
docs(note): 添加题解:1143. 最长公共子序列
1 parent 22b56de commit 7b36381

File tree

2 files changed

+98
-0
lines changed

2 files changed

+98
-0
lines changed

markdown/hello/0000.Hello.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@
7777
| <span style="color: #00AF9B;">**Easy**</span> | [**0905.按奇偶排序数组**](../easy/0905.按奇偶排序数组.md) |
7878
| <span style="color: #FFB822;">**Medium**</span> | [**0958.二叉树的完全性检验**](../medium/0958.二叉树的完全性检验.md) |
7979
| <span style="color: #00AF9B;">**Easy**</span> | [**1137.第N个泰波那契数**](../easy/1137.第N个泰波那契数.md) |
80+
| <span style="color: #FFB822;">**Medium**</span> | [**1143.最长公共子序列**](../medium/1143.最长公共子序列.md) |
8081
| <span style="color: #FFB822;">**Medium**</span> | [**1419.数青蛙**](../medium/1419.数青蛙.md) |
8182
| <span style="color: #00AF9B;">**Easy**</span> | [**1460.通过翻转子数组使两个数组相等**](../easy/1460.通过翻转子数组使两个数组相等.md) |
8283
| <span style="color: #00AF9B;">**Easy**</span> | [**1464.数组中两元素的最大乘积**](../easy/1464.数组中两元素的最大乘积.md) |
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
<h1 style="text-align: center;"> <span style="color: #FFB822;">1143. 最长公共子序列</span> </h1>
2+
3+
### 🚀 LeetCode
4+
5+
<base target="_blank">
6+
7+
<span style="color: #FFB822;">**Medium**</span> [**https://leetcode.cn/problems/longest-common-subsequence/**](https://leetcode.cn/problems/longest-common-subsequence/)
8+
9+
---
10+
11+
### ❓ Description
12+
13+
<br/>
14+
15+
给定两个字符串 `text1``text2`,返回这两个字符串的最长 **公共子序列** 的长度。
16+
17+
如果不存在 **公共子序列**,返回 `0`
18+
19+
一个字符串的 **子序列** 是指这样一个新的字符串:
20+
21+
它是由原字符串在不改变字符的相对顺序的情况下删除某些字符(也可以不删除任何字符)后组成的新字符串。
22+
23+
* 例如,`"ace"``"abcde"` 的子序列,但 `"aec"` 不是 `"abcde"` 的子序列。
24+
25+
两个字符串的 **公共子序列** 是这两个字符串所共同拥有的子序列。
26+
27+
<br/>
28+
29+
**示例 1:**
30+
31+
```
32+
输入: text1 = "abcde", text2 = "ace"
33+
输出: 3
34+
解释: 最长公共子序列是 "ace", 它的长度为 3
35+
```
36+
37+
**示例 2:**
38+
39+
```
40+
输入: text1 = "abc", text2 = "abc"
41+
输出: 3
42+
解释: 最长公共子序列是 "abc", 它的长度为 3
43+
```
44+
45+
**示例 3:**
46+
47+
```
48+
输入: text1 = "abc", text2 = "def"
49+
输出: 0
50+
解释: 两个字符串没有公共子序列, 返回 0
51+
```
52+
53+
<br/>
54+
55+
**提示:**
56+
57+
* `1 <= text1.length, text2.length <= 1000`
58+
* `text1``text2` 仅由小写英文字符组成
59+
60+
---
61+
62+
### ❗ Solution(动态规划)
63+
64+
<br/>
65+
66+
#### Java
67+
68+
```
69+
class Solution {
70+
public int longestCommonSubsequence(String text1, String text2) {
71+
int len1 = text1.length();
72+
int len2 = text2.length();
73+
74+
// 新建动态规划数组 (dp[i][j] 表示 text1[0:i] 和 text2[0:j] 的最长公共子序列)
75+
// 初始化第一行、第一列的值为 0 (由于 int 默认值就是 0, 不需要手动再初始化一次)
76+
int[][] dp = new int[len1 + 1][len2 + 1];
77+
78+
for (int i = 1; i <= len1; i++) {
79+
char c1 = text1.charAt(i - 1);
80+
for (int j = 1; j <= len2; j++) {
81+
char c2 = text2.charAt(j - 1);
82+
if (c1 == c2) {
83+
// 如果两个字符相等, 最长公共子序列的长度 + 1
84+
dp[i][j] = dp[i - 1][j - 1] + 1;
85+
} else {
86+
// 如果两个字符不相等, 当前位置的最长公共子序列的长度, 取以下两个长度的较大值:
87+
// text1[0:i-1] 与 text2[0:j] 的最长公共子序列的长度
88+
// text1[0:i] 与 text2[0:j-1] 的最长公共子序列的长度
89+
dp[i][j] = Math.max(dp[i - 1][j], dp[i][j - 1]);
90+
}
91+
}
92+
}
93+
94+
return dp[len1][len2];
95+
}
96+
}
97+
```

0 commit comments

Comments
 (0)