【一天一道LeetCode】#72. Edit Distance

一天一道LeetCode

本系列文章已全部上传至我的github,地址:ZeeCoder‘s Github

欢迎大家关注我的新浪微博,我的新浪微博

欢迎转载,转载请注明出处

(一)题目

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) Replace a character

(二)解题

这道题一拿到就觉得需要用到动态规划,于是考虑到用递归来解决,三种情况,依次进行,可是随着递归的加深,自然而然就超时了!

/*
注:此代码没有过多测试,如有错误,请各位留言指出
*/
class Solution {
public:
    int minDis = 1000000;
    int minDistance(string word1, string word2) {
        vector<int> minpath(10000,10000);
        getMinDis(word1,word2,0,0,minpath);
        return minDis;
    }
    void getMinDis(string word1, string word2, int idx, int count,vector<int>& minpath)
    {
        if(minpath[idx] <= count) return; //为了减少递归深度,可是还是超时了
        if (word1 == word2) { minDis = minDis < count ? minDis : count; return; }
        //如果该位上相等则继续
        if(idx < word1.size() && idx < word2.size() && word1[idx] == word2[idx]) getMinDis(word1, word2, idx + 1, count,minpath);
        else {
            if (idx < word1.size())//idx小于word1的时候才能删除
            {
                string tword1 = word1;
                tword1.erase(tword1.begin() + idx);
                getMinDis(tword1, word2, idx, count + 1,minpath);
            }
            if (idx < word2.size()) //插入话需要idx小于Word2的长度
            {
                string tword1 = word1;
                tword1.insert(idx, 1, word2[idx]);
                getMinDis(tword1, word2, idx + 1, count + 1,minpath);
            }
            if (idx < word1.size() && idx < word2.size())//替换则需要都小于
            {
                string tword1 = word1;
                tword1[idx] = word2[idx];
                getMinDis(tword1, word2, idx + 1, count + 1,minpath);
            }
        }
    }
};

下面,主角出现了!看到这个算法真正觉得算法的美妙了,由繁化简!

首先我们定义一个数组,dp[i][j],这个数组代表了word1的0~i转换到word2的0~j需要的最小步数。很显然,该矩阵应该初始化为:dp[0][i] = i和dp[i][0] = i,如下图(以ACE->ADEF为例):

那么,下面我们来看看动态规划最重要的状态转移方程。

1、插入操作:

dp[1][1]表示从A到A,dp[0][1]表示从”“到A,那么word1插入一个A就得到A,所以dp[1][1] = dp[0][1]+1

2、删除操作:

dp[1][1]表示从A到A,dp[1][0]表示从A到””,那么word1需要删除一个A,所以dp[1][1] = dp[1][0]+1

3、替换操作:

dp[1][1]表示从A到A\,dp[0][0]表示从”“到”“,那么dp[1][1] = dp[0][0];

dp[2][2]表示从AC到AD,需要替换操作,所以dp[2][2] = dp[1][1]+1;

看到这里大概都明白了这个算法的步骤了,dp[1][1]到底等于多少呢?

答案显而易见,dp[1][1]等于三者中的最小值。

算法到最后,矩阵dp得值看下图:

说这么多,代码见真招!

class Solution {
public:
    int minDistance(string word1, string word2) {
        int row = word1.length();
        int col = word2.length();
        //初始化
        vector<vector<int>> dpath(row+1,vector<int>(col+1,0));
        for(int i = 0 ; i < col+1 ; i++)/
        {
            dpath[0][i] = i;
        }
        for(int i = 0 ; i < row+1 ;i++)
        {
            dpath[i][0] = i;
        }
        for(int i = 1; i < row+1 ;i++)
        {
            for(int j = 1 ; j < col+1;j++)
            {
                //三者取最小
                dpath[i][j] = min(dpath[i-1][j]+1,dpath[i][j-1]+1);
                dpath[i][j] = min(dpath[i][j],dpath[i-1][j-1]+(word1[i-1] == word2[j-1]?0:1));
            }
        }
        return dpath[row][col];
    }
};
时间: 2024-11-12 16:50:24

【一天一道LeetCode】#72. Edit Distance的相关文章

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

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

第十八周 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

19.2.13 [LeetCode 72] Edit Distance

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: Input: word1 = "h

【leetcode】Edit Distance 详解

下图为TI C6xx DSP Nyquist总线拓扑图,总线连接了master与slave,提供了高速的数据传输.有很多种速率不同的总线,如图中的红色方框,最高速总线为CPU/2 TeraNet SCR(即VBUSM SCR),带宽为256bit,其他低速总线为CPU/3,CPU/6,带宽参考图中所示.总线之间用Bridge(桥)连接,作用包括转换总线的速率,使之与所流向总线的速率相同等. 在具体应用中,各种速率的总线完全可以满足复杂的数据传输,而数据传输的瓶颈往往在于连接总线之间的Bridge

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

刷题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.第一列中,d

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: