【leetcode】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 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的更多相关文章
- 【leetcode】Edit Distance (hard)
Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2 ...
- 【LeetCode】【动态规划】Edit Distance
描述 Given two words word1 and word2, find the minimum number of operations required to convert word1 ...
- 【LeetCode】Hamming Distance
问题网址 https://leetcode.com/problems/hamming-distance/ 就是一个异或后,求1的位数的问题. 看到问题之后,首先困扰是: int能不能求异或?是不是要转 ...
- 【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 ...
- 【LeetCode】字符串 string(共112题)
[3]Longest Substring Without Repeating Characters (2019年1月22日,复习) [5]Longest Palindromic Substring ( ...
- 【LeetCode】863. All Nodes Distance K in Binary Tree 解题报告(Python)
[LeetCode]863. All Nodes Distance K in Binary Tree 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http ...
- 【LeetCode】849. Maximize Distance to Closest Person 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【LeetCode】二叉查找树 binary search tree(共14题)
链接:https://leetcode.com/tag/binary-search-tree/ [220]Contains Duplicate III (2019年4月20日) (好题) Given ...
- 【LeetCode】位运算 bit manipulation(共32题)
[78]Subsets 给了一个 distinct 的数组,返回它所有的子集. Example: Input: nums = [,,] Output: [ [], [], [], [,,], [,], ...
随机推荐
- css样式中遇到!important
链接: http://zhidao.baidu.com/link?url=XyYHS2l-bFkzEgMBWfbQYuEV4vmGz8kOhj-jDL_HBqu0KnVIBQJKvw5OOO-bVjv ...
- Orchard源码分析(4):Orchard.Environment.OrchardStarter类
概述 OrchardStarter类是个静态类,主要作用是配置Autofac容器以及创建IOrchardHost(DefaultOrchardHost)对象,分别对应CreateHost和Create ...
- SQL Server2008 with(lock)用法
参考资料: WITH (NOLOCK) 在sqlserver 中with(nolock)详解
- JavaScript内置对象与原型继承
(一) 理解JavaScript类定义 1>关于内置对象理解 console.log(Date.prototype.__proto__===Object.prototype //tru ...
- cad中关于点样式点的绘制
点样式 从0开始, 默认的就是0 0= 一个小点; 1= 空的, 什么都不显示; 2= +加号; 3= X 叉号 设置点样式的命令是: pdmode: 可以假设认为是: point default m ...
- CF459A Pashmak and Garden (水
Pashmak and Garden Codeforces Round #261 (Div. 2) A. Pashmak and Garden time limit per test 1 second ...
- c# 字符串前加@
@在c#中为强制不转义的符号,在里面的转义字符无效. 例如:Console.WriteLine("你好\t吗?"); Console.WriteLine(@"你好\t吗& ...
- Eclipse将引用了第三方jar包的Java项目打包成jar文件的两种方法
方案一:用Eclipse自带的Export功能 步骤1:准备主清单文件 “MANIFEST.MF”, 由于是打包引用了第三方jar包的Java项目,故需要自定义配置文件MANIFEST.MF,在该项目 ...
- 基于iSCSI的SQL Server 2012群集测试(四)--模拟群集故障转移
6.模拟群集故障转移 6.1 模拟手动故障转移(1+1) 模拟手动故障转移的目的有以下几点: 测试群集是否能正常故障转移 测试修改端口是否能同步到备节点 测试禁用full-text和Browser服务 ...
- 动态调用web服务
通常我们在程序中需要调用WebService时,都是通过“添加Web引用”,让VS.NET环境来为我们生成服务代理,然后调用对应的Web服务.这样是使工作简单了,但是却和提供Web服务的URL.方法名 ...