Skip to content

Commit f99a761

Browse files
committed
feat: add solutions to lc problems: No.0392, 1143
1 parent 42352ce commit f99a761

File tree

12 files changed

+232
-15
lines changed

12 files changed

+232
-15
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -196,9 +196,9 @@
196196
- [最长回文子序列](.solution/0500-0599/0516.Longest%20Palindromic%20Subsequence/README.md)
197197
- [最长递增子序列](./solution/0300-0399/0300.Longest%20Increasing%20Subsequence/README.md)
198198
- [摆动序列](./solution/0300-0399/0376.Wiggle%20Subsequence/README.md)
199+
- [最长公共子序列](./solution/1100-1199/1143.Longest%20Common%20Subsequence/README.md)
199200
- [礼物的最大价值](./lcof/面试题47.%20礼物的最大价值/README.md)
200201
- [俄罗斯套娃信封问题](./solution/0300-0399/0354.Russian%20Doll%20Envelopes/README.md)
201-
- [最长公共子序列](./solution/1100-1199/1143.Longest%20Common%20Subsequence/README.md)
202202

203203
### 回溯算法
204204

README_EN.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -190,8 +190,8 @@ Complete solutions to [LeetCode](https://leetcode.com/problemset/all/), [LCOF](h
190190
- [Longest Palindromic Subsequence](.solution/0500-0599/0516.Longest%20Palindromic%20Subsequence/README_EN.md)
191191
- [Longest Increasing Subsequence](./solution/0300-0399/0300.Longest%20Increasing%20Subsequence/README_EN.md)
192192
- [Wiggle Subsequence](./solution/0300-0399/0376.Wiggle%20Subsequence/README_EN.md)
193-
- [Russian Doll Envelopes](./solution/0300-0399/0354.Russian%20Doll%20Envelopes/README_EN.md)
194193
- [Longest Common Subsequence](./solution/1100-1199/1143.Longest%20Common%20Subsequence/README_EN.md)
194+
- [Russian Doll Envelopes](./solution/0300-0399/0354.Russian%20Doll%20Envelopes/README_EN.md)
195195

196196
### Backtracking
197197

solution/0300-0399/0392.Is Subsequence/README.md

+15
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,21 @@ public:
108108
};
109109
```
110110
111+
### **Go**
112+
113+
```go
114+
func isSubsequence(s string, t string) bool {
115+
i, j, m, n := 0, 0, len(s), len(t)
116+
for i < m && j < n {
117+
if s[i] == t[j] {
118+
i++
119+
}
120+
j++
121+
}
122+
return i == m
123+
}
124+
```
125+
111126
### **...**
112127

113128
```

solution/0300-0399/0392.Is Subsequence/README_EN.md

+15
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,21 @@ public:
8383
};
8484
```
8585
86+
### **Go**
87+
88+
```go
89+
func isSubsequence(s string, t string) bool {
90+
i, j, m, n := 0, 0, len(s), len(t)
91+
for i < m && j < n {
92+
if s[i] == t[j] {
93+
i++
94+
}
95+
j++
96+
}
97+
return i == m
98+
}
99+
```
100+
86101
### **...**
87102

88103
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Solution {
2+
public:
3+
bool isSubsequence(string s, string t) {
4+
int m = s.size(), n = t.size();
5+
int i = 0, j = 0;
6+
while (i < m && j < n) {
7+
if (s[i] == t[j]) {
8+
++i;
9+
}
10+
++j;
11+
}
12+
return i == m;
13+
}
14+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
func isSubsequence(s string, t string) bool {
2+
i, j, m, n := 0, 0, len(s), len(t)
3+
for i < m && j < n {
4+
if s[i] == t[j] {
5+
i++
6+
}
7+
j++
8+
}
9+
return i == m
10+
}

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

+62-5
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@
6262

6363
递推公式如下:
6464

65-
![](./images/gif.gif)
65+
![](https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/1100-1199/1143.Longest%20Common%20Subsequence/images/gif.gif)
6666

6767
<!-- tabs:start -->
6868

@@ -77,8 +77,11 @@ class Solution:
7777
dp = [[0] * (n + 1) for _ in range(m + 1)]
7878
for i in range(1, m + 1):
7979
for j in range(1, n + 1):
80-
dp[i][j] = dp[i - 1][j - 1] + 1 if text1[i - 1] == text2[j - 1] else max(dp[i - 1][j], dp[i][j - 1])
81-
return dp[m][n]
80+
if text1[i - 1] == text2[j - 1]:
81+
dp[i][j] = dp[i - 1][j - 1] + 1
82+
else:
83+
dp[i][j] = max(dp[i - 1][j], dp[i][j - 1])
84+
return dp[-1][-1]
8285
```
8386

8487
### **Java**
@@ -92,15 +95,69 @@ class Solution {
9295
int[][] dp = new int[m + 1][n + 1];
9396
for (int i = 1; i <= m; ++i) {
9497
for (int j = 1; j <= n; ++j) {
95-
char c1 = text1.charAt(i - 1), c2 = text2.charAt(j - 1);
96-
dp[i][j] = c1 == c2 ? dp[i - 1][j - 1] + 1 : Math.max(dp[i - 1][j], dp[i][j - 1]);
98+
if (text1.charAt(i - 1) == text2.charAt(j - 1)) {
99+
dp[i][j] = dp[i - 1][j - 1] + 1;
100+
} else {
101+
dp[i][j] = Math.max(dp[i - 1][j], dp[i][j - 1]);
102+
}
97103
}
98104
}
99105
return dp[m][n];
100106
}
101107
}
102108
```
103109

110+
### **C++**
111+
112+
```cpp
113+
class Solution {
114+
public:
115+
int longestCommonSubsequence(string text1, string text2) {
116+
int m = text1.size(), n = text2.size();
117+
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]) {
121+
dp[i][j] = dp[i - 1][j - 1] + 1;
122+
} else {
123+
dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
124+
}
125+
}
126+
}
127+
return dp[m][n];
128+
}
129+
};
130+
```
131+
132+
### **Go**
133+
134+
```go
135+
func longestCommonSubsequence(text1 string, text2 string) int {
136+
m, n := len(text1), len(text2)
137+
dp := make([][]int, m+1)
138+
for i := 0; i <= m; i++ {
139+
dp[i] = make([]int, n+1)
140+
}
141+
for i := 1; i <= m; i++ {
142+
for j := 1; j <= n; j++ {
143+
if text1[i-1] == text2[j-1] {
144+
dp[i][j] = dp[i-1][j-1] + 1
145+
} else {
146+
dp[i][j] = max(dp[i-1][j], dp[i][j-1])
147+
}
148+
}
149+
}
150+
return dp[m][n]
151+
}
152+
153+
func max(a, b int) int {
154+
if a > b {
155+
return a
156+
}
157+
return b
158+
}
159+
```
160+
104161
### **...**
105162

106163
```

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

+63-4
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@
5050

5151
## Solutions
5252

53+
Dynamic programming.
54+
5355
<!-- tabs:start -->
5456

5557
### **Python3**
@@ -61,8 +63,11 @@ class Solution:
6163
dp = [[0] * (n + 1) for _ in range(m + 1)]
6264
for i in range(1, m + 1):
6365
for j in range(1, n + 1):
64-
dp[i][j] = dp[i - 1][j - 1] + 1 if text1[i - 1] == text2[j - 1] else max(dp[i - 1][j], dp[i][j - 1])
65-
return dp[m][n]
66+
if text1[i - 1] == text2[j - 1]:
67+
dp[i][j] = dp[i - 1][j - 1] + 1
68+
else:
69+
dp[i][j] = max(dp[i - 1][j], dp[i][j - 1])
70+
return dp[-1][-1]
6671
```
6772

6873
### **Java**
@@ -74,12 +79,66 @@ class Solution {
7479
int[][] dp = new int[m + 1][n + 1];
7580
for (int i = 1; i <= m; ++i) {
7681
for (int j = 1; j <= n; ++j) {
77-
char c1 = text1.charAt(i - 1), c2 = text2.charAt(j - 1);
78-
dp[i][j] = c1 == c2 ? dp[i - 1][j - 1] + 1 : Math.max(dp[i - 1][j], dp[i][j - 1]);
82+
if (text1.charAt(i - 1) == text2.charAt(j - 1)) {
83+
dp[i][j] = dp[i - 1][j - 1] + 1;
84+
} else {
85+
dp[i][j] = Math.max(dp[i - 1][j], dp[i][j - 1]);
86+
}
87+
}
88+
}
89+
return dp[m][n];
90+
}
91+
}
92+
```
93+
94+
### **C++**
95+
96+
```cpp
97+
class Solution {
98+
public:
99+
int longestCommonSubsequence(string text1, string text2) {
100+
int m = text1.size(), n = text2.size();
101+
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]) {
105+
dp[i][j] = dp[i - 1][j - 1] + 1;
106+
} else {
107+
dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
108+
}
79109
}
80110
}
81111
return dp[m][n];
82112
}
113+
};
114+
```
115+
116+
### **Go**
117+
118+
```go
119+
func longestCommonSubsequence(text1 string, text2 string) int {
120+
m, n := len(text1), len(text2)
121+
dp := make([][]int, m+1)
122+
for i := 0; i <= m; i++ {
123+
dp[i] = make([]int, n+1)
124+
}
125+
for i := 1; i <= m; i++ {
126+
for j := 1; j <= n; j++ {
127+
if text1[i-1] == text2[j-1] {
128+
dp[i][j] = dp[i-1][j-1] + 1
129+
} else {
130+
dp[i][j] = max(dp[i-1][j], dp[i][j-1])
131+
}
132+
}
133+
}
134+
return dp[m][n]
135+
}
136+
137+
func max(a, b int) int {
138+
if a > b {
139+
return a
140+
}
141+
return b
83142
}
84143
```
85144

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
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+
for (int j = 1; j <= n; ++j) {
8+
if (text1[i - 1] == text2[j - 1]) {
9+
dp[i][j] = dp[i - 1][j - 1] + 1;
10+
} else {
11+
dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
12+
}
13+
}
14+
}
15+
return dp[m][n];
16+
}
17+
};
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+
}

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

+5-2
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,11 @@ public int longestCommonSubsequence(String text1, String text2) {
44
int[][] dp = new int[m + 1][n + 1];
55
for (int i = 1; i <= m; ++i) {
66
for (int j = 1; j <= n; ++j) {
7-
char c1 = text1.charAt(i - 1), c2 = text2.charAt(j - 1);
8-
dp[i][j] = c1 == c2 ? dp[i - 1][j - 1] + 1 : Math.max(dp[i - 1][j], dp[i][j - 1]);
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+
}
912
}
1013
}
1114
return dp[m][n];

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

+5-2
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,8 @@ def longestCommonSubsequence(self, text1: str, text2: str) -> int:
44
dp = [[0] * (n + 1) for _ in range(m + 1)]
55
for i in range(1, m + 1):
66
for j in range(1, n + 1):
7-
dp[i][j] = dp[i - 1][j - 1] + 1 if text1[i - 1] == text2[j - 1] else max(dp[i - 1][j], dp[i][j - 1])
8-
return dp[m][n]
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]

0 commit comments

Comments
 (0)