Skip to content

Commit 270394b

Browse files
Merge pull request youngyangyang04#2140 from Lozakaka/patch-28
新增java:用最長公共子序列反推
2 parents fe7ecca + c25a765 commit 270394b

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
@@ -189,6 +189,31 @@ class Solution {
189189
}
190190
}
191191
```
192+
```java
193+
//DP - longest common subsequence (用最長公共子序列反推)
194+
class Solution {
195+
public int minDistance(String word1, String word2) {
196+
char[] char1 = word1.toCharArray();
197+
char[] char2 = word2.toCharArray();
198+
199+
int len1 = char1.length;
200+
int len2 = char2.length;
201+
202+
int dp[][] = new int [len1 + 1][len2 + 1];
203+
204+
for(int i = 1; i <= len1; i++){
205+
for(int j = 1; j <= len2; j++){
206+
if(char1[i - 1] == char2[j - 1])
207+
dp[i][j] = dp[i - 1][j - 1] + 1;
208+
else
209+
dp[i][j] = Math.max(dp[i - 1][j], dp[i][j - 1]);
210+
}
211+
}
212+
213+
return len1 + len2 - (2 * dp[len1][len2]);//和leetcode 1143只差在這一行。
214+
}
215+
}
216+
```
192217

193218

194219
Python:

0 commit comments

Comments
 (0)