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 00aed2a

Browse files
更新 0583.两个字符串的删除操作.md Java代码
添加另一种动态规划思路
1 parent 0ba48bc commit 00aed2a

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,28 @@ public:
128128
129129
Java:
130130
```java
131+
// dp数组中存储word1和word2最长相同子序列的长度
132+
class Solution {
133+
public int minDistance(String word1, String word2) {
134+
int len1 = word1.length();
135+
int len2 = word2.length();
136+
int[][] dp = new int[len1 + 1][len2 + 1];
137+
138+
for (int i = 1; i <= len1; i++) {
139+
for (int j = 1; j <= len2; j++) {
140+
if (word1.charAt(i - 1) == word2.charAt(j - 1)) {
141+
dp[i][j] = dp[i - 1][j - 1] + 1;
142+
} else {
143+
dp[i][j] = Math.max(dp[i - 1][j], dp[i][j - 1]);
144+
}
145+
}
146+
}
147+
148+
return len1 + len2 - dp[len1][len2] * 2;
149+
}
150+
}
151+
152+
// dp数组中存储需要删除的字符个数
131153
class Solution {
132154
public int minDistance(String word1, String word2) {
133155
int[][] dp = new int[word1.length() + 1][word2.length() + 1];

0 commit comments

Comments
(0)

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