题解 | #编辑距离(一)#
编辑距离(一)
https://www.nowcoder.com/practice/6a1483b5be1547b1acd7940f867be0da
using System; using System.Collections.Generic; class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param str1 string字符串 * @param str2 string字符串 * @return int整型 */ public int editDistance (string str1, string str2) { int[,] dp = new int[str1.Length + 1, str2.Length + 1]; for(int i = 0; i <= str1.Length; i++){ dp[i,0] = i; } for(int j = 0; j <= str2.Length; j++){ dp[0,j] = j; } for(int i = 1; i <= str1.Length; i++){ //Console.WriteLine(); //Console.WriteLine("i:"+i); for(int j = 1; j <= str2.Length; j++){ if(str1[i - 1] == str2[j - 1]) dp[i,j] = dp[i-1,j-1]; else dp[i,j] = Math.Min(dp[i-1,j-1], Math.Min(dp[i-1,j], dp[i,j-1])) + 1; //Console.Write("j:"+ j + "value" + dp[i,j]+" "); } } return dp[str1.Length, str2.Length]; } }