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

Appearance settings

Latest commit

 

History

History
History
34 lines (34 loc) · 1.05 KB

File metadata and controls

34 lines (34 loc) · 1.05 KB
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
package leetcode.dp;
/*
* 最短编辑距离
*
* 字符串
* */
public class leetcode42 {
public static void main(String[] args) {
new Solution42().minDistance("happy","hsppay");
}
}
class Solution42 {
public int minDistance(String word1, String word2) {
char [] array1=word1.toCharArray();
char [] array2=word2.toCharArray();
int [][] dp=new int[array1.length+1][array2.length+1];
for (int i = 0; i <=array1.length; i++) {
dp[i][0]=i;
}
for (int i = 0; i <=array2.length ; i++) {
dp[0][i]=i;
}
for (int i = 1; i <=array1.length ; i++) {
for (int j = 1; j <= array2.length; j++) {
if (array1[i-1]==array2[j-1]){ //i j的字符相同
dp[i][j]=Math.min(dp[i-1][j-1],Math.min(dp[i-1][j]+1,dp[i][j-1]+1));
}else{
dp[i][j]=Math.min(dp[i-1][j-1]+1,Math.min(dp[i-1][j]+1,dp[i][j-1]+1));
}
}
}
return dp[array1.length][array2.length];
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.