Skip to content

Commit 841e15d

Browse files
committed
feat: add solutions to lcof2 problem: No.095
1 parent 450ce41 commit 841e15d

File tree

9 files changed

+190
-13
lines changed

9 files changed

+190
-13
lines changed

lcof2/剑指 Offer II 095. 最长公共子序列/README.md

+90-1
Original file line numberDiff line numberDiff line change
@@ -58,22 +58,111 @@
5858

5959
<!-- 这里可写通用的实现逻辑 -->
6060

61+
动态规划法。
62+
63+
定义 `dp[i][j]` 表示 `text1[0:i-1]``text2[0:j-1]` 的最长公共子序列(闭区间)。
64+
65+
递推公式如下:
66+
67+
![](https://cdn.jsdelivr.net/gh/doocs/leetcode@main/lcof2/剑指%20Offer%20II%20095.%20最长公共子序列/images/gif.gif)
68+
6169
<!-- tabs:start -->
6270

6371
### **Python3**
6472

6573
<!-- 这里可写当前语言的特殊实现逻辑 -->
6674

6775
```python
68-
76+
class Solution:
77+
def longestCommonSubsequence(self, text1: str, text2: str) -> int:
78+
m, n = len(text1), len(text2)
79+
dp = [[0] * (n + 1) for _ in range(m + 1)]
80+
for i in range(1, m + 1):
81+
for j in range(1, n + 1):
82+
if text1[i - 1] == text2[j - 1]:
83+
dp[i][j] = dp[i - 1][j - 1] + 1
84+
else:
85+
dp[i][j] = max(dp[i - 1][j], dp[i][j - 1])
86+
return dp[-1][-1]
6987
```
7088

7189
### **Java**
7290

7391
<!-- 这里可写当前语言的特殊实现逻辑 -->
7492

7593
```java
94+
class Solution {
95+
public int longestCommonSubsequence(String text1, String text2) {
96+
int m = text1.length(), n = text2.length();
97+
int[][] dp = new int[m + 1][n + 1];
98+
for (int i = 1; i <= m; ++i) {
99+
for (int j = 1; j <= n; ++j) {
100+
if (text1.charAt(i - 1) == text2.charAt(j - 1)) {
101+
dp[i][j] = dp[i - 1][j - 1] + 1;
102+
} else {
103+
dp[i][j] = Math.max(dp[i - 1][j], dp[i][j - 1]);
104+
}
105+
}
106+
}
107+
return dp[m][n];
108+
}
109+
}
110+
```
111+
112+
### **C++**
113+
114+
```cpp
115+
class Solution {
116+
public:
117+
int longestCommonSubsequence(string text1, string text2) {
118+
int m = text1.size(), n = text2.size();
119+
vector<vector<int>> dp(m + 1, vector<int>(n + 1));
120+
for (int i = 1; i <= m; ++i)
121+
{
122+
for (int j = 1; j <= n; ++j)
123+
{
124+
if (text1[i - 1] == text2[j - 1])
125+
{
126+
dp[i][j] = dp[i - 1][j - 1] + 1;
127+
}
128+
else
129+
{
130+
dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
131+
}
132+
}
133+
}
134+
return dp[m][n];
135+
}
136+
};
137+
```
76138
139+
### **Go**
140+
141+
```go
142+
func longestCommonSubsequence(text1 string, text2 string) int {
143+
m, n := len(text1), len(text2)
144+
dp := make([][]int, m+1)
145+
for i := 0; i <= m; i++ {
146+
dp[i] = make([]int, n+1)
147+
}
148+
for i := 1; i <= m; i++ {
149+
for j := 1; j <= n; j++ {
150+
if text1[i-1] == text2[j-1] {
151+
dp[i][j] = dp[i-1][j-1] + 1
152+
} else {
153+
dp[i][j] = max(dp[i-1][j], dp[i][j-1])
154+
}
155+
}
156+
}
157+
return dp[m][n]
158+
}
159+
160+
func max(a, b int) int {
161+
if a > b {
162+
return a
163+
}
164+
return b
165+
}
77166
```
78167

79168
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class Solution {
2+
public:
3+
int longestCommonSubsequence(string text1, string text2) {
4+
int m = text1.size(), n = text2.size();
5+
vector<vector<int>> dp(m + 1, vector<int>(n + 1));
6+
for (int i = 1; i <= m; ++i)
7+
{
8+
for (int j = 1; j <= n; ++j)
9+
{
10+
if (text1[i - 1] == text2[j - 1])
11+
{
12+
dp[i][j] = dp[i - 1][j - 1] + 1;
13+
}
14+
else
15+
{
16+
dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
17+
}
18+
}
19+
}
20+
return dp[m][n];
21+
}
22+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
func longestCommonSubsequence(text1 string, text2 string) int {
2+
m, n := len(text1), len(text2)
3+
dp := make([][]int, m+1)
4+
for i := 0; i <= m; i++ {
5+
dp[i] = make([]int, n+1)
6+
}
7+
for i := 1; i <= m; i++ {
8+
for j := 1; j <= n; j++ {
9+
if text1[i-1] == text2[j-1] {
10+
dp[i][j] = dp[i-1][j-1] + 1
11+
} else {
12+
dp[i][j] = max(dp[i-1][j], dp[i][j-1])
13+
}
14+
}
15+
}
16+
return dp[m][n]
17+
}
18+
19+
func max(a, b int) int {
20+
if a > b {
21+
return a
22+
}
23+
return b
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution {
2+
public int longestCommonSubsequence(String text1, String text2) {
3+
int m = text1.length(), n = text2.length();
4+
int[][] dp = new int[m + 1][n + 1];
5+
for (int i = 1; i <= m; ++i) {
6+
for (int j = 1; j <= n; ++j) {
7+
if (text1.charAt(i - 1) == text2.charAt(j - 1)) {
8+
dp[i][j] = dp[i - 1][j - 1] + 1;
9+
} else {
10+
dp[i][j] = Math.max(dp[i - 1][j], dp[i][j - 1]);
11+
}
12+
}
13+
}
14+
return dp[m][n];
15+
}
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Solution:
2+
def longestCommonSubsequence(self, text1: str, text2: str) -> int:
3+
m, n = len(text1), len(text2)
4+
dp = [[0] * (n + 1) for _ in range(m + 1)]
5+
for i in range(1, m + 1):
6+
for j in range(1, n + 1):
7+
if text1[i - 1] == text2[j - 1]:
8+
dp[i][j] = dp[i - 1][j - 1] + 1
9+
else:
10+
dp[i][j] = max(dp[i - 1][j], dp[i][j - 1])
11+
return dp[-1][-1]
Loading

solution/1100-1199/1143.Longest Common Subsequence/README.md

+9-4
Original file line numberDiff line numberDiff line change
@@ -115,11 +115,16 @@ public:
115115
int longestCommonSubsequence(string text1, string text2) {
116116
int m = text1.size(), n = text2.size();
117117
vector<vector<int>> dp(m + 1, vector<int>(n + 1));
118-
for (int i = 1; i <= m; ++i) {
119-
for (int j = 1; j <= n; ++j) {
120-
if (text1[i - 1] == text2[j - 1]) {
118+
for (int i = 1; i <= m; ++i)
119+
{
120+
for (int j = 1; j <= n; ++j)
121+
{
122+
if (text1[i - 1] == text2[j - 1])
123+
{
121124
dp[i][j] = dp[i - 1][j - 1] + 1;
122-
} else {
125+
}
126+
else
127+
{
123128
dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
124129
}
125130
}

solution/1100-1199/1143.Longest Common Subsequence/README_EN.md

+9-4
Original file line numberDiff line numberDiff line change
@@ -99,11 +99,16 @@ public:
9999
int longestCommonSubsequence(string text1, string text2) {
100100
int m = text1.size(), n = text2.size();
101101
vector<vector<int>> dp(m + 1, vector<int>(n + 1));
102-
for (int i = 1; i <= m; ++i) {
103-
for (int j = 1; j <= n; ++j) {
104-
if (text1[i - 1] == text2[j - 1]) {
102+
for (int i = 1; i <= m; ++i)
103+
{
104+
for (int j = 1; j <= n; ++j)
105+
{
106+
if (text1[i - 1] == text2[j - 1])
107+
{
105108
dp[i][j] = dp[i - 1][j - 1] + 1;
106-
} else {
109+
}
110+
else
111+
{
107112
dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
108113
}
109114
}

solution/1100-1199/1143.Longest Common Subsequence/Solution.cpp

+9-4
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,16 @@ class Solution {
33
int longestCommonSubsequence(string text1, string text2) {
44
int m = text1.size(), n = text2.size();
55
vector<vector<int>> dp(m + 1, vector<int>(n + 1));
6-
for (int i = 1; i <= m; ++i) {
7-
for (int j = 1; j <= n; ++j) {
8-
if (text1[i - 1] == text2[j - 1]) {
6+
for (int i = 1; i <= m; ++i)
7+
{
8+
for (int j = 1; j <= n; ++j)
9+
{
10+
if (text1[i - 1] == text2[j - 1])
11+
{
912
dp[i][j] = dp[i - 1][j - 1] + 1;
10-
} else {
13+
}
14+
else
15+
{
1116
dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
1217
}
1318
}

0 commit comments

Comments
 (0)