首页 > 每天刷一道牛客题霸-第22天- 最小编辑代价
头像
菜鸟也要飞的高
编辑于 2021-01-05 10:38
+ 关注

每天刷一道牛客题霸-第22天- 最小编辑代价

题目

https://www.nowcoder.com/practice/05fed41805ae4394ab6607d0d745c8e4?tpId=190&&tqId=35213&rp=1&ru=/ta/job-code-high-rd&qru=/ta/job-code-high-rd/question-ranking

import java.util.*;


public class Solution {
    /**
     * min edit cost
     * @param str1 string字符串 the string
     * @param str2 string字符串 the string
     * @param ic int整型 insert cost
     * @param dc int整型 delete cost
     * @param rc int整型 replace cost
     * @return int整型
     */
    public static int minEditCost (String str1, String str2, int ic, int dc, int rc) {
        int[][] count = new int[str1.length()+1][str2.length()+1];
        for (int i = 0; i < count.length; i++) {
            count[i][0] = i*dc;
        }
        for (int i = 0; i < count[0].length; i++) {
            count[0][i] = i*ic;
        }
        for (int i = 1; i < count.length; i++) {
            for (int j = 1; j < count[i].length; j++) {
                if (str1.charAt(i-1) == str2.charAt(j-1)) {
                    count[i][j] = count[i-1][j-1];
                }else{
                    count[i][j] =Math.min(count[i][j-1]+ ic,  Math.min(count[i-1][j-1] + rc, count[i-1][j] + dc));
                }
            }
        }
        return count[str1.length()][str2.length()];
        // write code here
    }

}

全部评论

(0) 回帖
加载中...
话题 回帖

推荐话题

相关热帖

近期精华帖

热门推荐