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. Semantic ui 学习笔记 持续更新

    这个semantic 更新版本好快~ 首先是代码的标识<code></code> 具体样式就是红框这样的 圈起来代码感觉不错 不过要在semantic.css里在加上如下样式~ ...

  2. Python之路【第十二篇】前端之js&dome&jQuery

    JavaScript是一种属于网络的脚本语言,已经被广泛用于Web应用开发,常用来为网页添加各式各样的动态功能,为用户提供更流畅美观的浏览效果.通常JavaScript脚本是通过嵌入在HTML中来实现 ...

  3. python 计算器的(正则匹配+递归)

    经过2天的长时间的战斗,python计算器终于完成了. import re val="1-2*((60-30*(9-2*5/3+7/3*99/4*2998+10*568/14))-(-4*3 ...

  4. C# 表达式树demo

    class Program { static void Main(string[] args) { //创建Expression参数 var left = System.Linq.Expression ...

  5. ServletContext中常用方法

    ..获取Tomcat的Context的初始化参数. 1.获取Tomcat的server.xml中设置Context的初始化参数. 例如: <Context path="/testcon ...

  6. 15分钟学会使用Git和远程代码库

    git是个了不起但却复杂的源代码管理系统.它能支持复杂的任务,却因此经常被认为太过复杂而不适用于简单的日常工作.让我们诚实一记吧:Git是复杂的,我们不要装作它不是.但我仍然会试图教会你用(我的)基本 ...

  7. C语言之strrchr函数

    from:http://blog.csdn.net/hgj125073/article/details/8443912 [FROM MSDN && 百科] 原型:char *strrc ...

  8. R You Ready?——大数据时代下优雅、卓越的统计分析及绘图环境

    作者按:本文根据去年11月份CSDN举办的“大数据技术大会”演讲材料整理,最初发表于2012年2月期<程序员>杂志. 0  R 的安装

  9. PHP中9大缓存技术总结

    1.全页面静态化缓存 也就是将页面全部生成html静态页面,用户访问时直接访问的静态页面,而不会去走php服务器解析的流程.此种方式,在CMS系统中比较常见,比如dedecms: 一种比较常用的实现方 ...

  10. twoSum

    Given an array of integers, find two numbers such that they add up to a specific target number. The ...