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

Commit c25a765

Browse files
新增java:用最長公共子序列反推
新增java:用最長公共子序列反推
1 parent 59a9280 commit c25a765

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

‎problems/0583.两个字符串的删除操作.md‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,31 @@ class Solution {
184184
}
185185
}
186186
```
187+
```java
188+
//DP - longest common subsequence (用最長公共子序列反推)
189+
class Solution {
190+
public int minDistance(String word1, String word2) {
191+
char[] char1 = word1.toCharArray();
192+
char[] char2 = word2.toCharArray();
193+
194+
int len1 = char1.length;
195+
int len2 = char2.length;
196+
197+
int dp[][] = new int [len1 + 1][len2 + 1];
198+
199+
for(int i = 1; i <= len1; i++){
200+
for(int j = 1; j <= len2; j++){
201+
if(char1[i - 1] == char2[j - 1])
202+
dp[i][j] = dp[i - 1][j - 1] + 1;
203+
else
204+
dp[i][j] = Math.max(dp[i - 1][j], dp[i][j - 1]);
205+
}
206+
}
207+
208+
return len1 + len2 - (2 * dp[len1][len2]);//和leetcode 1143只差在這一行。
209+
}
210+
}
211+
```
187212

188213

189214
Python:

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /