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

思路:

这个去年学算法的时候学过,当时觉得好难啊,现在一看,这题真简单。就是一道常规的动态规划题目。直接AC,高兴~~

用dp[m][n]存储word1的前m个字符与word2的前n个字符的最小匹配距离。

那么dp[i][j] = dp[i-1][j]+1 (word1删除一个字符)、dp[i][j-1] + 1 (word2删除一个字符)、 dp[i-1][j-1]  + ((word1[i-1]==word2[j-1]) ? 0(当前字符相等) : 1(替换))) 中最小的值。

class Solution {
public:
int minDistance(string word1, string word2) {
int len1 = word1.length();
int len2 = word2.length(); vector<vector<int>> dp(len1 + , vector<int>(len2 + , ));
for(int i = ; i < len1 + ; i++)
{
dp[i][] = i;
}
for(int j = ; j < len2 + ; j++)
{
dp[][j] = j;
}
for(int i = ; i < len1 + ; i++)
{
for(int j = ;j < len2 + ; j++)
{
dp[i][j] = min(min(dp[i-][j] + , dp[i][j-] + ), dp[i-][j-] + ((word1[i-]==word2[j-]) ? : ));
}
}
return dp[len1][len2];
}
};

【leetcode】Edit Distance (hard)的更多相关文章

  1. 【leetcode】Edit Distance

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

  2. 【LeetCode】【动态规划】Edit Distance

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

  3. 【LeetCode】Hamming Distance

    问题网址 https://leetcode.com/problems/hamming-distance/ 就是一个异或后,求1的位数的问题. 看到问题之后,首先困扰是: int能不能求异或?是不是要转 ...

  4. 【leetcode】1184. Distance Between Bus Stops

    题目如下: A bus has n stops numbered from 0 to n - 1 that form a circle. We know the distance between al ...

  5. 【LeetCode】字符串 string(共112题)

    [3]Longest Substring Without Repeating Characters (2019年1月22日,复习) [5]Longest Palindromic Substring ( ...

  6. 【LeetCode】863. All Nodes Distance K in Binary Tree 解题报告(Python)

    [LeetCode]863. All Nodes Distance K in Binary Tree 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http ...

  7. 【LeetCode】849. Maximize Distance to Closest Person 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  8. 【LeetCode】二叉查找树 binary search tree(共14题)

    链接:https://leetcode.com/tag/binary-search-tree/ [220]Contains Duplicate III (2019年4月20日) (好题) Given ...

  9. 【LeetCode】位运算 bit manipulation(共32题)

    [78]Subsets 给了一个 distinct 的数组,返回它所有的子集. Example: Input: nums = [,,] Output: [ [], [], [], [,,], [,], ...

随机推荐

  1. 移动WebApp利用Chrome浏览器进行调试

    详细的请看这个(HBuilder是我长期使用,而且值得支持的国内前端开发编辑器) http://ask.dcloud.net.cn/article/151 http://ask.dcloud.net. ...

  2. [转载]解析用户生命周期价值:LTV

    http://www.sykong.com/2014/07/23144 http://youxiputao.com/articles/1288 http://www.woshipm.com/opera ...

  3. [译]View components and Inject in ASP.NET MVC 6

    原文:http://www.asp.net/vnext/overview/aspnet-vnext/vc 介绍view components view components (VCs) 类似于part ...

  4. iOS: ARC & MRC下string内存管理策略探究

    ARC & MRC下string内存管理策略探究 前两天跟同事争论一个关于NSString执行copy操作以后是否会发生变化,两个人整了半天,最后写代码验证了一下,发现原来NSString操作 ...

  5. Hadoop 之Mong DB 之CentOS 6 使用 yum 安装MongoDB及服务器端配置

    安装MongoDB的方法有很多种,可以源代码安装,在Centos也可以用yum源安装的方法.由于MongoDB更新得比较快,我比较喜欢用yum源安装的方法.64位Centos下的安装步骤如下: 1.准 ...

  6. 【PHP面向对象(OOP)编程入门教程】6.如何去使用对象中的成员

    上面看到PHP对象中的成员有两种一种是成员属性, 一种是成员方法.对象我们以经可以声明了,$p1=new Person(); 怎么去使用对象的成员呢?要想访问对象中的成员就要使用一个特殊的操作符”-& ...

  7. DateEdit和TimeEdit用法

    DateEdit 控件默认情况下,显示的只有日期,没有时间.下面介绍2中日期和时间同时显示的方法: 1.Properties.VistaDisplayMode 为true, 2.Properties. ...

  8. CDN技术发展趋势

    智能化 存储智能化 关键技术点:文件分片热度的判断策略,跨节点快速文件调度 存储向以文件切片为基础的全网共享存储发展:把一个整片切成n个小块,按用户访问热度分散存储在不同节点.当用户访问的时候,由边缘 ...

  9. 21个免费的UI设计工具和资源网站,不管是web,js,android都

    本帖最后由 hua631150873 于 2014-9-12 18:26 编辑 Lumzy 官方地址:http://www.lumzy.com/ Lumzy是一个网站应用和原型界面制作工具.使用Lum ...

  10. 新建Oracle数据库时,提示使用database control配置数据库时,要求在当前oracle主目录中配置监听程序

    新建一个oracle数据库时,当提示使用database control配置数据库时,要求在当前oracle主目录中配置监听程序等字样的时候,问题是那个监听的服务没有启动,解决方法如下: 打开cmd命 ...