题解 | #牛群编号变更#
牛群编号变更
https://www.nowcoder.com/practice/9295f0f796b34793832710d5c939a619
import java.util.*; public class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param word1 string字符串 * @param word2 string字符串 * @return int整型 */ public int minDistance (String word1, String word2) { // write code here int l1 = word1.length(); int l2 = word2.length(); int[][] dp = new int[l1+1][l2+1]; for(int i=0;i<=l1;i++){ dp[i][0]=i; } for(int j=0;j<=l2;j++){ dp[0][j] = j; } for(int i=1;i<=l1;i++){ for(int j=1;j<=l2;j++){ dp[i][j] = Math.min(dp[i-1][j],dp[i][j-1])+1; if(word1.charAt(i-1)==word2.charAt(j-1)){ dp[i][j] = Math.min(dp[i][j],dp[i-1][j-1]); } } } return dp[l1][l2]; } }