题解 | #牛群编号变更#

牛群编号变更

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];
    }
}

全部评论

相关推荐

05-09 14:45
门头沟学院 Java
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务