所用代码 java
dp[i] [j]:以i-1为结尾的word1 和 以j-1为结尾的word2为结尾 为了让两个字符串相同最少操作次数为dp[i] [j]
递推公式:
相同 if(word1[i-1] == word2[j-1]) dp[i][j] = dp[i-1][j-1]
不相同 dp[i][j] = min(dp[i-1][j] + 1, dp[i][j-1] + 1, dp[i-1][j-1] + 2)
dp[i-1][j] + 1
dp[i][j-1] + 1
dp[i-1][j-1] + 2
其实就相当于先删word2 dp[i][j] = dp[i][j-1] + 1
,然后再删word1 dp[i][j-1] = dp[i-1][j-1] + 1
初始化:
dp[0][j] = j
:相当于word1为空字符串,所以要删j(word2.length)个元素dp[i][0] = i
:相当于word2为空字符串,所以要删i(word1.length)个元素dp[0][0] = 0
:空字符与空字符匹配遍历方向
打印
class Solution {public int minDistance(String word1, String word2) {int n1 = word1.length();int n2 = word2.length();int[][] dp = new int[n1+1][n2+1];// 初始化// dp[i][0],相当于word2为空串,就相当于word1有几个字母就要删几个for (int i = 0; i <= n1; i++) {dp[i][0] = i;}// dp[0][j],相当于word1为空串,就相当于word2有几个字母就要删几个for (int j = 0; j <= n2; j++) {dp[0][j] = j;}for (int i = 1; i <= n1; i++) {for (int j = 1; j <= n2; j++) {// 相等就不用删除,和i-1,j-1一样if (word1.charAt(i-1) == word2.charAt(j-1)){dp[i][j] = dp[i-1][j-1];}else {// 不相等就需要把word1删一个,或者word2删一个// 或者两个都要删一个,就相当于其中一个先删除一个字母,然后再删除一个字母dp[i][j] = Math.min(dp[i-1][j] + 1, dp[i][j-1] + 1);}}
// System.out.println(Arrays.toString(dp[i]));}return dp[n1][n2];}
}
打印结果:
Finished:Your input:"leetcode""etco"Output:4Expected:4stdout:0 1 2 3 4[1, 2, 3, 4, 5][2, 1, 2, 3, 4][3, 2, 3, 4, 5][4, 3, 2, 3, 4][5, 4, 3, 2, 3][6, 5, 4, 3, 2][7, 6, 5, 4, 3][8, 7, 6, 5, 4]
本题还有一种方法,因为他和之前的最长公共子序列类似,我们第一步找到最长公共子序列,这个子序列就是两者需要保留的最大长度,然后用两者的len减最长公共子序列就是需要删除的少步骤。
class Solution {public int minDistance(String word1, String word2) {int n1 = word1.length();int n2 = word2.length();int[][] dp = new int[n1+1][n2+1];for (int i = n1; i > 0; i--) {for (int j = n2; j > 0; j--) {if (word1.charAt(i-1) == word2.charAt(j-1)){dp[i][j] = dp[i-1][j-1] + 1;}else {dp[i][j] = Math.max(dp[i-1][j], dp[i][j-1]);}}
// System.out.println(Arrays.toString(dp[i]));}return word1.length() - dp[n1][n2] + word2.length() - dp[n1][n2];}
}
dp[i] [j]:以i-1为结尾的word1和以j-1为结尾的word2,最少的操作次数为dp[i] [j]
递推公式:
相同,不需要操作 if(word[i-1] == word[j-1]) dp[i][j] = dp[i-1][j-1]
不同
min(dp[i-1][j] + 1, dp[i][j-1] + 1)
dp[i][j] = dp[i-1][j-1] + 1
初始化:
dp[i][0]=i
word2空串需操作i次,就是删i个字符dp[0][j]=j
word1空串需操作j次,就是删j个字符遍历顺序
打印dp
class Solution {public int minDistance(String word1, String word2) {int n1 = word1.length();int n2 = word2.length();int[][] dp = new int[n1+1][n2+1];// 初始化// dp[i][0],相当于word2为空串,所以word1有几个字母就得删几个(word2添加几个)for (int i = 0; i <= n1; i++) {dp[i][0] = i;}// dp[0][j],相当于word1为空串,word2有几个字母就要删几个(或者word1添加几个)for (int j = 0; j <= n2; j++) {dp[0][j] = j;}
// System.out.println(Arrays.toString(dp[0]));
for (int i = 1; i <= n1; i++) {for (int j = 1; j <= n2; j++) {if (word1.charAt(i-1) == word2.charAt(j-1)){// 相等的情况就不操作,取上一次相等时候的值dp[i][j] = dp[i-1][j-1];}else {// 不相等主要有两个操作,一是删(word1和word2都可以删,增和删一样里面那个min),// 二是改dp[i-1][j-1] + 1dp[i][j] = Math.min(Math.min(dp[i-1][j] + 1, dp[i][j-1]+ 1), dp[i-1][j-1] + 1);}}
// System.out.println(Arrays.toString(dp[i]));}return dp[n1][n2];}
}
打印结果:第一行为word1为空的情况,即word2有几个元素就需删几个;第一列为word1为空的情况,即word2有几个元素就要删除几个元素。
Your input:"horse""ros"Output:3Expected:3stdout:[0, 1, 2, 3][1, 1, 2, 3][2, 2, 1, 2][3, 2, 2, 2][4, 3, 3, 2][5, 4, 4, 3]
下一篇:设计模式之门面模式(外观模式)