(没思路,很典型,重要

给定两个单词 word1 和 word2,计算出将 word1 转换成 word2 所使用的最少操作数 。

你可以对一个单词进行如下三种操作:

  1. 插入一个字符
  2. 删除一个字符
  3. 替换一个字符

示例 1:

输入: word1 = "horse", word2 = "ros"
输出: 3
解释:
horse -> rorse (将 'h' 替换为 'r')
rorse -> rose (删除 'r')
rose -> ros (删除 'e')

示例 2:

输入: word1 = "intention", word2 = "execution"
输出: 5
解释:
intention -> inention (删除 't')
inention -> enention (将 'i' 替换为 'e')
enention -> exention (将 'n' 替换为 'x')
exention -> exection (将 'n' 替换为 'c')
exection -> execution (插入 'u')

关键:

dp[i][j]代表由word1的前i个子串变为word2的前j个子串的花费

在删除,插入,修改中取花费最小的那个:dp[i][j] = Math.min(Math.min(dp[i-1][j], dp[i][j-1]), dp[i-1][j-1]) + 1;

链接:https://www.nowcoder.com/questionTerminal/81d7738f954242e5ade5e65ec40e5027
来源:牛客网 public class Solution {
    public int minDistance(String word1, String word2) {
        if(word1 == null && word2 == null)
            return 0;
        if(word1 == null)
            return word2.length();
        if(word2 == null)
            return word1.length();
         
        // dp[i][j]代表由word1的前i个子串变为word2的前j个子串的花费
        // 其中i,j均可为0,代表空串:""
        int[][] dp = new int[word1.length() + 1][word2.length() + 2];
        dp[0][0] = 0;
        // 首先初始化两个子串中有一个为空串的情况
        for(int i = 1; i <= word1.length(); i++){
            dp[i][0] = i;
        }
        for(int j = 1; j <= word2.length(); j++){
            dp[0][j] = j;
        }
         
        for(int i = 1; i <= word1.length(); i++){
            for(int j = 1; j <= word2.length(); j++){
                // 如果这两个字符相等,那么交由上一种情况处理,如abc,dfc
                // 这种情况与ab,df花费是一样的
                // 不然就要在删除,插入,修改中取花费最小的那个
                if(word1.charAt(i - 1) == word2.charAt(j - 1))
                    dp[i][j] = dp[i-1][j-1];
                else
                    dp[i][j] = Math.min(Math.min(dp[i-1][j], dp[i][j-1]), dp[i-1][j-1]) + 1;
            }
        }
        return dp[word1.length()][word2.length()];
    }
}

我:

    public int minDistance(String word1, String word2) {
int length1 = word1.length();
int length2 = word2.length(); int[][] dp = new int[length1+1][length2+1];
for (int i=0;i<=length1;i++) {
dp[i][0] = i;
}
for (int i=0;i<=length2;i++) {
dp[0][i] = i;
}
for (int i=1;i<=length1;i++) {
for (int j=1;j<=length2;j++) {
if (word1.charAt(i-1) == word2.charAt(j-1)) {
dp[i][j] = dp[i-1][j-1];
} else {
dp[i][j] = 1 + Math.min(dp[i-1][j-1],Math.min(dp[i][j-1],dp[i-1][j]));
}
}
}
return dp[length1][length2];
}

【1】【leetcode-72 动态规划】 编辑距离的更多相关文章

  1. Leetcode之动态规划(DP)专题-72. 编辑距离(Edit Distance)

    Leetcode之动态规划(DP)专题-72. 编辑距离(Edit Distance) 给定两个单词 word1 和 word2,计算出将 word1 转换成 word2 所使用的最少操作数 . 你可 ...

  2. leetcode笔记 动态规划在字符串匹配中的应用

    目录 leetcode笔记 动态规划在字符串匹配中的应用 0 参考文献 1. [10. Regular Expression Matching] 1.1 题目 1.2 思路 && 解题 ...

  3. Leetcode之动态规划(DP)专题-详解983. 最低票价(Minimum Cost For Tickets)

    Leetcode之动态规划(DP)专题-983. 最低票价(Minimum Cost For Tickets) 在一个火车旅行很受欢迎的国度,你提前一年计划了一些火车旅行.在接下来的一年里,你要旅行的 ...

  4. Leetcode之动态规划(DP)专题-647. 回文子串(Palindromic Substrings)

    Leetcode之动态规划(DP)专题-647. 回文子串(Palindromic Substrings) 给定一个字符串,你的任务是计算这个字符串中有多少个回文子串. 具有不同开始位置或结束位置的子 ...

  5. Leetcode之动态规划(DP)专题-474. 一和零(Ones and Zeroes)

    Leetcode之动态规划(DP)专题-474. 一和零(Ones and Zeroes) 在计算机界中,我们总是追求用有限的资源获取最大的收益. 现在,假设你分别支配着 m 个 0 和 n 个 1. ...

  6. Leetcode之动态规划(DP)专题-486. 预测赢家(Predict the Winner)

    Leetcode之动态规划(DP)专题-486. 预测赢家(Predict the Winner) 给定一个表示分数的非负整数数组. 玩家1从数组任意一端拿取一个分数,随后玩家2继续从剩余数组任意一端 ...

  7. Leetcode之动态规划(DP)专题-264. 丑数 II(Ugly Number II)

    Leetcode之动态规划(DP)专题-264. 丑数 II(Ugly Number II) 编写一个程序,找出第 n 个丑数. 丑数就是只包含质因数 2, 3, 5 的正整数. 示例: 输入: n ...

  8. Leetcode之动态规划(DP)专题-198. 打家劫舍(House Robber)

    Leetcode之动态规划(DP)专题-198. 打家劫舍(House Robber) 你是一个专业的小偷,计划偷窃沿街的房屋.每间房内都藏有一定的现金,影响你偷窃的唯一制约因素就是相邻的房屋装有相互 ...

  9. Leetcode之动态规划(DP)专题-121. 买卖股票的最佳时机(Best Time to Buy and Sell Stock)

    Leetcode之动态规划(DP)专题-121. 买卖股票的最佳时机(Best Time to Buy and Sell Stock) 股票问题: 121. 买卖股票的最佳时机 122. 买卖股票的最 ...

  10. Leetcode之动态规划(DP)专题-122. 买卖股票的最佳时机 II(Best Time to Buy and Sell Stock II)

    Leetcode之动态规划(DP)专题-122. 买卖股票的最佳时机 II(Best Time to Buy and Sell Stock II) 股票问题: 121. 买卖股票的最佳时机 122. ...

随机推荐

  1. zookeeper(一) 原理

    参考文档:http://cailin.iteye.com/blog/2014486/ http://www.uml.org.cn/zjjs/201707282.asp?artid=19686 一.zo ...

  2. 设计模式概要 & 六原则一法则

    参考文章 http://blog.csdn.net/sinat_26342009/article/details/46419873 继承vs组合:http://www.cnblogs.com/feic ...

  3. 【Gamma】Scrum Meeting 5

    目录 写在前面 进度情况 任务进度表 照片 写在前面 例会时间:6.1 22:45-23:00 例会地点:微信群语音通话 代码进度记录github在这里 临近期末,团队成员课程压力均较大,需要较多时间 ...

  4. [Gamma阶段]第四次Scrum Meeting

    Scrum Meeting博客目录 [Gamma阶段]第四次Scrum Meeting 基本信息 名称 时间 地点 时长 第四次Scrum Meeting 19/05/30 大运村寝室6楼 35min ...

  5. 使用iis部署asp.net core 中的坑

    使用vs2017创建asp.net core mvc 模板网站然后部署到iis上,如果在一直有并发访问的情况下,操作iis重新绑定域名操作,网站会直接挂掉,重启应用池也没用,而且部署到iis上容易出现 ...

  6. 深度学习最全优化方法总结比较及在tensorflow实现

    版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明. 本文链接:https://blog.csdn.net/u010899985/article/d ...

  7. Oracle 导出表结构

    Oracle导出表结构 select A.Table_Name 表名 , -- A.column_name 字段名 A.column_name 字段名, A.data_type 数据类型, A.dat ...

  8. 【C++】C++中explicity关键字的使用

    读者可以尝试预言一下这段代码的输出: #include <iostream> using namespace std; class Complex { private: double re ...

  9. CSAGAN的几大重点 - 2

    1.生成器 1)MRU(SketchyGAN) 计算过程为: 与DCGAN[46]和ResNet生成架构的定性和定量比较可以在5.3节中找到.MRU块有两个输入:输入特征图xi和图像I,输出特征图yi ...

  10. elementUI vue this.$confirm 和el-dialog 弹出框 移动

    调试了好久, 还能凑合用, 请直接看DOME 示例,复制就能用: <!DOCTYPE html> <html lang="zh"> <head> ...