刷题72. Edit Distance

一、题目说明

题目72. Edit Distance,计算将word1转换为word2最少需要的操作。操作包含:插入一个字符,删除一个字符,替换一个字符。本题难度为Hard!

二、我的解答

这个题目一点思路也没,就直接看答案了。用的还是dp算法,dp[n1+1][n2+1]中的dp[i][j]表示将word1的前i位,变为word2的前j位需要的步骤。注意第1行是空,第1列也是空。

1.第一行中,dp[0][i]表示空字符""到word2[0,...,i]需要编辑几次

2.第一列中,dp[i][0]表示空字符到word2[0,...,i]需要编辑几次

3.循环计算dp的值

if(word1[i]==word2[j]){
    dp[i][j] == dp[i-1][j-1]
}else{
    dp[i][j]=min(dp[i?1][j?1],dp[i][j?1],dp[i?1][j])+1
}

有了方法,实现不难:

class Solution{
    public:
        int minDistance(string word1,string word2){
            int n1 = word1.size(),n2= word2.size();
            if(n1<=0) return n2;
            if(n2<=0) return n1;
            vector<vector<int>> dp(n1+1,vector<int>(n2+1,0));
            //初始化第1行
            for(int i=0;i<=n2;i++){
                dp[0][i] = i;
            }

            //初始化第1列
            for(int i=0;i<=n1;i++){
                dp[i][0] = i;
            }

            //计算dp矩阵
//          if(word1[i]==word2[j]){
//              dp[i][j] == dp[i-1][j-1]
//          }else{
//              dp[i][j]=min(dp[i-1][j-1],dp[i][j-1],dp[i-1][j])+1
//          }
            for(int i=1;i<=n1;i++){//行
                for(int j=1;j<=n2;j++){//列
                    if(word1[i-1]==word2[j-1]) {
                        dp[i][j] = dp[i-1][j-1];
                    }else{
                        dp[i][j] = min(min(dp[i-1][j],dp[i][j-1]),dp[i-1][j-1])+1;
                    }
                }
            }
            return dp[n1][n2];
        }
};

性能如下:

Runtime: 16 ms, faster than 40.38% of C++ online submissions for Edit Distance.
Memory Usage: 11.3 MB, less than 62.50% of C++ online submissions for Edit Distance.

三、优化措施

今天做这么多吧,有点晕了。明天继续!

再回头看看题目Edit Distance,我好像以前做过,不过忘记了。

原文地址:https://www.cnblogs.com/siweihz/p/12249622.html

时间: 2024-10-10 20:30:54

刷题72. Edit Distance的相关文章

LintCode刷题笔记-- Edit distance

描述: Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2. (each operation is counted as 1 step.) You have the following 3 operations permitted on a word: Insert a character Delete a character Replace a

72. Edit Distance &amp;&amp; 161. One Edit Distance

72. Edit Distance Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2. (each operation is counted as 1 step.) You have the following 3 operations permitted on a word: a) Insert a characterb) Delete a c

LeetCode 72. Edit Distance Java

72.Edit Distance(编辑距离) 题目: 给定两个单词 word1 和 word2,计算出将 word1 转换成 word2 所使用的最少操作数 . 你可以对一个单词进行如下三种操作: 插入一个字符 删除一个字符 替换一个字符 思路: 多次选择试图得到最优解,那么考虑动态规划. 先假设word1有len1位,word2有len2位,建立数组step,step[i][j]就代表我们要将word1前 i 位转换为word2前 j 位的最少数量. 此时word1查找到第 i+1 位字母a,

leetCode 72.Edit Distance (编辑距离) 解题思路和方法

Edit Distance Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2. (each operation is counted as 1 step.) You have the following 3 operations permitted on a word: a) Insert a character b) Delete a char

Baozi Leetcode solution 72. Edit Distance

Problem Statement Given two words word1 and word2, find the minimum number of operations required to convert word1 to word2. You have the following 3 operations permitted on a word: Insert a character Delete a character Replace a character Example 1:

LeetCode 72 Edit Distance

Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2. (each operation is counted as 1 step.) You have the following 3 operations permitted on a word: a) Insert a character b) Delete a character c) Repla

72. Edit Distance

Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2. (each operation is counted as 1 step.) You have the following 3 operations permitted on a word: a) Insert a characterb) Delete a characterc) Replace

第十八周 Leetcode 72. Edit Distance(HARD) O(N^2)DP

Leetcode72 看起来比较棘手的一道题(列DP方程还是要大胆猜想..) DP方程该怎么列呢? dp[i][j]表示字符串a[0....i-1]转化为b[0....j-1]的最少距离 转移方程分三种情况考虑 分别对应三中操作 因为只需要三个值就可以更新dp[i][j] 我们可以把空间复杂度降低到O(n) Replace word1[i - 1] by word2[j - 1] (dp[i][j] = dp[i - 1][j - 1] + 1 (for replacement)); Delet

72. Edit Distance (String; DP)

Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2. (each operation is counted as 1 step.) You have the following 3 operations permitted on a word: a) Insert a characterb) Delete a characterc) Replace