题解 | #编辑距离(一)#
编辑距离(一)
https://www.nowcoder.com/practice/6a1483b5be1547b1acd7940f867be0da
/** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param str1 string字符串 * @param str2 string字符串 * @return int整型 */ #include <string.h> int Mini(int a,int b) { if(a>b) return b; else return a; } int editDistance(char* str1, char* str2 ) { // write code here int n1,n2,i,j; n1=strlen(str1); n2=strlen(str2); int dp[n1+1][n2+1]; dp[0][0]=0; for(i=1;i<n1+1;i++) { dp[i][0]=i; } for(j=1;j<n2+1;j++) { dp[0][j]=j; } for(i=1;i<n1+1;i++) { for(j=1;j<n2+1;j++) { if(str1[i-1]==str2[j-1]) dp[i][j]=dp[i-1][j-1]; else dp[i][j]=Mini(dp[i-1][j-1], Mini(dp[i-1][j],dp[i][j-1]))+1; } } return dp[n1][n2]; }