Edit Distance 题解

原创文章,拒绝转载

题目来源:https://leetcode.com/problems/edit-distance/description/


Description

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

Solution

class Solution {
public:
int minDistance(string word1, string word2) {
if (word1 == word2)
return 0;
if (word1.empty())
return word2.length();
if (word2.empty())
return word1.length(); int len1 = word1.length() + 1;
int len2 = word2.length() + 1;
int** f = new int*[len1];
int i, j;
for (i = 0; i < len1; i++) {
f[i] = new int[len2];
f[i][0] = i;
} for (j = 0; j < len2; j++) {
f[0][j] = j;
} for (i = 1; i < len1; i++) {
for (j = 1; j < len2; j++) {
if (word1[i - 1] == word2[j - 1]) {
f[i][j] = f[i - 1][j - 1];
} else {
f[i][j] = min(min(f[i - 1][j] + 1, f[i][j - 1] + 1), f[i - 1][j - 1] + 1);
}
}
}
int res = f[len1 - 1][len2 - 1]; for (i = 0; i < len1; i++)
delete [] f[i];
delete [] f; return res;
}
};

解题描述

这道题是动态规划中经典的编辑距离问题,关键之处在于将求算总的编辑的距离这个大问题转换成每一步比较两个字符串中指定位置上的字符的时候应该得到的编辑距离f[i][j]。增加、删除、替换都是相对上一步编辑距离+1,那关键就是上一步应该选择哪一步?很明显就是选择之前的编辑距离最少的一步,即f[i][j] = min(min(f[i - 1][j] + 1, f[i][j - 1] + 1), f[i - 1][j - 1] + 1)的意义;如果指定位上的字符相等,那显然就有f[i][j] = f[i - 1][j - 1]

[Leetcode Week8]Edit Distance的更多相关文章

  1. [LeetCode] One Edit Distance 一个编辑距离

    Given two strings S and T, determine if they are both one edit distance apart. 这道题是之前那道Edit Distance ...

  2. Java for LeetCode 072 Edit Distance【HARD】

    Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2 ...

  3. LeetCode One Edit Distance

    原题链接在这里:https://leetcode.com/problems/one-edit-distance/ Given two strings S and T, determine if the ...

  4. [LeetCode] 72. Edit Distance 编辑距离

    Given two words word1 and word2, find the minimum number of operations required to convert word1 to  ...

  5. 【leetcode】Edit Distance

    Edit Distance Given two words word1 and word2, find the minimum number of steps required to convert  ...

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

    Edit Distance Given two words word1 and word2, find the minimum number of steps required to convert  ...

  7. [LeetCode] 72. Edit Distance(最短编辑距离)

    传送门 Description Given two words word1 and word2, find the minimum number of steps required to conver ...

  8. LeetCode - 72. Edit Distance

    最小编辑距离,动态规划经典题. Given two words word1 and word2, find the minimum number of steps required to conver ...

  9. 【leetcode】Edit Distance (hard)

    Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2 ...

随机推荐

  1. ES6 语法糖

    重新认识ES6中的语法糖:https://segmentfault.com/a/1190000010159725

  2. [leetcode-636-Exclusive Time of Functions]

    Given the running logs of n functions that are executed in a nonpreemptive single threaded CPU, find ...

  3. 常量表达式 & constexpr

    [常量表达式] 一个这样的表达式:值不会改变 && 在编译过程中就能够得到计算结果 常见的常量表达式:字面值.用常量表达式初始化的const对象 一个对象是不是常量表达式由它的数据类型 ...

  4. poi excel导出 xssf 带下拉框

    需求:导出之后带有二级级联的下拉框.(类似于省市). 最初的思路是怀疑是不是数组内串太多了,导出之后的excel有36行,调试的误区在于刚开始认为对行数有限制,后自己写了一个测试类,才发现不是行数,而 ...

  5. 【EasyNetQ】- 多态发布和订阅

    您可以订阅接口,然后发布该接口的实现. 我们来看一个例子.我有一个接口IAnimal和两个实现Cat和Dog: public interface IAnimal { string Name { get ...

  6. AGC017C Snuke and Spells(巧妙的线段覆盖模型)

    题目大意: 给出n个球,每个球上都有数字,然后每次都进行如下操作 如果当前的球总共有k个,那么就把球上数字为k的所有球都消除掉 注意到,并不是每种情况都可以全部消光,所以你可以选择若干球,把它们标号改 ...

  7. hihocoder 1465 循环串匹配问题(后缀自动机)

    后缀自动机感觉好万能 tries图和ac自动机能做的,后缀自动机很多也都可以做 这里的循环匹配则是后缀自动机能做的另一个神奇功能 循环匹配意思就是S是abba, T是abb 问'abb', 'bba' ...

  8. 算法学习——kruskal重构树

    kruskal重构树是一个比较冷门的数据结构. 其实可以看做一种最小生成树的表现形式. 在普通的kruskal中,如果一条边连接了在2个不同集合中的点的话,我们将合并这2个点所在集合. 而在krusk ...

  9. [Leetcode] rotate image 旋转图片

    You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise). ...

  10. C++——继承时的this指针

    1.this指针只在类的成员函数中使用,当类的成员函数需要用到自己的指针时就要用到this指针.但静态函数不能使用this关键字,其解释是:因为this是个引用,哪个对象调用方法就引用哪个对象. 而静 ...