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. 【赛后补题】(HDU6228) Tree {2017-ACM/ICPC Shenyang Onsite}

    这条题目当时卡了我们半天,于是成功打铁--今天回来一看,mmp,贪心思想怎么这么弱智.....(怪不得场上那么多人A了 题意分析 这里是原题: Tree Time Limit: 2000/1000 M ...

  2. sphinx调用API参考(官方手册)

    API的参考实现是用PHP写成的,因为(我们相信)较之其他语言,Sphinx在PHP中应用最广泛.因此这份参考文档基于PHP API的参考,而且这节中的所有的代码样例都用PHP给出. 当然,其他所有A ...

  3. Mybatis 异常记录(1): Invalid bound statement (not found)

    错误信息: org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): com.pingan.cr ...

  4. 问题 A: 完数

    问题 A: 完数 时间限制: 1 Sec  内存限制: 32 MB提交: 252  解决: 178[提交][状态][讨论版][命题人:外部导入] 题目描述 求1-n内的完数,所谓的完数是这样的数,它的 ...

  5. LeetCode - 70. Climbing Stairs(0ms)

    You are climbing a stair case. It takes n steps to reach to the top. Each time you can either climb ...

  6. BZOJ 4595 SHOI2015 激光发生器 射线,线段,偏转

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=4595 题意概述: 给出一条射线和N条线段,射线遇到线段会发生反射,令入射角alpha,出射 ...

  7. Python不同进制之间的转换

    不同的进制 二进制    0b101 以数字0和字母b打头的表示二进制数 如果出现大于等于2的数 会抛出SyntaxError异常 八进制    0711 以数字0打头的数字表示八进制数 如果出现大于 ...

  8. Performanced C++ 经验规则

    http://www.cnblogs.com/ccdev/archive/2012/12/27/2836448.html Performanced C++,意为“高性能C++“编程,是笔者和所在团队多 ...

  9. SSH答疑解惑系列(一)——spring容器是如何启动的

    SSH框架十分受欢迎,其中有一个原因就是spring可以和Struts2框架无缝整合.在使用spring时,无需手动创建web容器,而是通过配置文件声明式创建spring容器. 在web应用中,创建s ...

  10. el-input怎么绑定回车事件

    在 Vue 2.0 中,为自定义组件绑定原生事件必须使用 .native 修饰符:<el-input v-model="queryForm.skuName" placehol ...