We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent c6d2776 commit b37bf8dCopy full SHA for b37bf8d
problems/为了绝杀编辑距离,卡尔做了三步铺垫.md
@@ -167,7 +167,33 @@ else {
167
168
169
Java:
170
-
+```java
171
+class Solution {
172
+ public int minDistance(String word1, String word2) {
173
+ int m = word1.length();
174
+ int n = word2.length();
175
+ int[][] dp = new int[m+1][n+1];
176
+ for(int i = 1; i <= m; i++){
177
+ dp[i][0] = i;
178
+ }
179
+ for(int i = 1; i <= n; i++){
180
+ dp[0][i] = i;
181
182
183
+ for(int j = 1; j <= n; j++){
184
+ int left = dp[i][j-1]+1;
185
+ int mid = dp[i-1][j-1];
186
+ int right = dp[i-1][j]+1;
187
+ if(word1.charAt(i-1) != word2.charAt(j-1)){
188
+ mid ++;
189
190
+ dp[i][j] = Math.min(left,Math.min(mid,right));
191
192
193
+ return dp[m][n];
194
195
+}
196
+```
197
198
Python:
199
0 commit comments