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

用m*n的矩阵vector中的(i,j)存储从word1中(0...i)到word2中(0...j)的最小变化次数

矩阵中,从上到下对应删除操作,从左到右对应插入操作,从左上到右下对应Replace操作。

class Solution {
public:
int minDistance(string word1, string word2) { //1->2
if(word1==word2)
return ;
int len1 = word1.size();
int len2 = word2.size();
vector<int> temp(len2+,);
vector<vector<int> > vec(len1+,temp); for(int i=;i<=len1;i++){
vec[i][] = i;
}
for(int j=;j<=len2;j++){
vec[][j] = j;
}
for(int i=;i<=len1;i++){
for(int j=;j<=len2;j++){
int tem1,tem2; tem1 = min(vec[i-][j]+,vec[i][j-]+);
tem2 = word1[i-]==word2[j-] ? vec[i-][j-]:vec[i-][j-]+;
vec[i][j] = min(tem1,tem2);
}//end for
}//end for
return vec[len1][len2];
}
};

[LeetCode] Edit Distance(很好的DP)的更多相关文章

  1. [LeetCode] Edit Distance 编辑距离

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

  2. Leetcode:Edit Distance 解题报告

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

  3. [leetcode]Edit Distance @ Python

    原题地址:https://oj.leetcode.com/problems/edit-distance/ 题意: Given two words word1 and word2, find the m ...

  4. LeetCode: Edit Distance && 子序列题集

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

  5. 编辑距离Edit Distance 非常典型的DP类型题目

    https://leetcode.com/problems/edit-distance/?tab=Description 真的非常好,也非常典型. https://discuss.leetcode.c ...

  6. Leetcode Edit Distance

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

  7. LeetCode——Edit Distance

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

  8. [LeetCode] Edit Distance 字符串变换为另一字符串动态规划

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

  9. Leetcode之动态规划(DP)专题-72. 编辑距离(Edit Distance)

    Leetcode之动态规划(DP)专题-72. 编辑距离(Edit Distance) 给定两个单词 word1 和 word2,计算出将 word1 转换成 word2 所使用的最少操作数 . 你可 ...

随机推荐

  1. 游戏 slider

    using UnityEngine; using System.Collections; public class La : MonoBehaviour { float verticalValue=0 ...

  2. ios 适应屏幕

    标准做法:auto layout 其中:也可以用代码判断  1.如果不是hardcode的话,又怎么会不兼容呢..  2.如果你全写320*480也是兼容吗 3.写320嘛的就是hardcode啊.  ...

  3. 升级到WP8必需知道的13个特性

    http://www.cnblogs.com/sonic1abc/archive/2012/11/28/2792467.html   Windows phone 8 SDK 已经发布一段时间了, 已经 ...

  4. centos 下如何加入sudo 用户

    当在终端执行sudo命令时,系统提示“ jackluo is not in the sudoers file”: $ sudo ls Password:jackluo is not in the su ...

  5. sql server导出insert语句

    在所需要导出数据库上右键 选择[任务] 然后选择[生成脚本] 选择数据库,点击下一步到[数据脚本选项] 编写数据的脚本 选择为true  这一步很重要 下一步选择要导出的对象 下一步选择表 点击完成 ...

  6. CreateFeatureClass 异常,尝试读取或写入受保护的内存 Access

    在创建要素时出现如下异常,百思不得其解. 后经过多次试验,发现文件名改为其他的就可以了.自出的文件名为"第3条",后将文件名改为"A3"等,则可正常创建. 后再 ...

  7. Linux环境下实现生产者消费者问题

    #include <stdio.h> #include <semaphore.h> #include <stdlib.h> #include <pthread ...

  8. 当PHP引擎试图实例化一个未知类的操作

    在PHP开发过程中,如果希望从外部引入一个class,通常会使用include和require方法,去把定义这个class的文件包含进来,但是这样可能会使得在引用文件的新脚本中,存在大量的includ ...

  9. IIS 301 跳转

    IIS设置301重定向 IIS服务器下做301永久重定向设置方法. IIS6设置301重定向: 1.新建一个站点,对应目录如E:\wwwroot\301web.该目录下只需要1个文件,即index.h ...

  10. 2. MySQL

    安装: apt-get install mysql-server   配置: mysql_install_db mysql_secure_installation   另有两处需要修改 [mysqld ...