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

采用动态规划求解

1. d[0, j] = j;

2. d[i, 0] = i;

3. d[i, j] = d[i-1, j - 1] if A[i] == B[j]

4. d[i, j] = min(d[i-1, j - 1], d[i, j - 1], d[i-1, j]) + 1  if A[i] != B[j]

 
 class Solution {
public:
int minDistance(string word1, string word2) { int n1=word1.length();
int n2=word2.length(); if(n1==) return n2;
if(n2==) return n1; //采用二维数组效率更高
//vector<vector<int> > dp(n1+1,vector<int>(n2+1)); int **dp=new int*[n1+];
for(int i=;i<n1+;i++)
{
dp[i]=new int[n2+];
} dp[][]=;
for(int i=;i<=n1;i++)
{
dp[i][]=i;
}
for(int j=;j<=n2;j++)
{
dp[][j]=j;
} for(int i=;i<=n1;i++)
{
for(int j=;j<=n2;j++)
{ if(word1[i-]==word2[j-])
{
dp[i][j]=dp[i-][j-];
}
else
{
dp[i][j]=min(dp[i-][j],min(dp[i][j-],dp[i-][j-]))+;
}
}
} int result=dp[n1][n2];
for(int i=;i<n1+;i++)
{
delete[] dp[i];
} return result;
}
};

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

  1. 【leetcode】Edit Distance (hard)

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

  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. Ajax– 刷新页面 【转】

    jquery刷新页面(局部及全页面刷新) 2009-12-31 11:39:32|  分类: javascript|举报|字号 订阅 局部刷新: 这个方法就多了去了,常见的有以下几种: $.get方法 ...

  2. 安装RabbitMQ遇到的问题

    消息队列RabbitMQ在安装的时候出现了问题.. 我这里是参考的 .NET 环境中使用RabbitMQ 进行安装的..首先声明 这篇博文没有问题.. 但是在我安装的时候发现..ErLang环境装完 ...

  3. eclipse-4.4.2安装Groovy插件(其他版本eclipse可参考)

    步骤 : 1.启动eclipse,点击help -> Install New Software... 在弹出的窗口中点击:Add... Groovy插件的地址:http://dist.sprin ...

  4. 【AngularJS】—— 10 指令的复用

    前面练习了如何自定义指令,这里练习一下指令在不同的控制器中如何复用. —— 来自<慕课网 指令3> 首先看一下一个小例子,通过自定义指令,捕获鼠标事件,并触发控制器中的方法. 单个控制器的 ...

  5. [Linux] Chang DNS Setting on Linux

    主机的虚拟机使用 NAT 模式时, NAT的DNS不好用.于是需要将虚拟机的DNS改成和主机的一样,这样虚拟机也可以请求互联网资源. Linux的DNS 服务器定义在 /etc/resolv.conf

  6. nyoj 14 会场安排问题(贪心专题)

    会场安排问题 时间限制:3000 ms  |  内存限制:65535 KB 难度:4   描述 学校的小礼堂每天都会有许多活动,有时间这些活动的计划时间会发生冲突,需要选择出一些活动进行举办.小刘的工 ...

  7. nyoj 171 聪明的kk

    聪明的kk 时间限制:1000 ms  |  内存限制:65535 KB 难度:3   描述 聪明的“KK”非洲某国展馆的设计灵感源于富有传奇色彩的沙漠中陡然起伏的沙丘,体现出本国不断变换和绚丽多彩的 ...

  8. poj3070 (斐波那契,矩阵快速幂)

    Fibonacci Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9630   Accepted: 6839 Descrip ...

  9. 安装wampserver 2.5的时候出现丢失MSVCR100.dll的解决办法。

    转载地址:http://www.mafutian.net/127.html

  10. Swift语法入门

    正文参考: https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Progra ...