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. POJ 3034 Whac-a-Mole(DP)

    题目链接 理解了题意之后,这个题感觉状态转移还是挺好想的,实现起来确实有点繁琐,代码能力还有待加强,经过很长时间才发现bug.注意坐标可能是负的. #include <cstdio> #i ...

  2. Get the image file(s) some informations,Including the Make,Model,Date/Time,etc

    This is a blog about how to get the image file(s) some informations.Including the Make,Model,Date/Ti ...

  3. web app

    *********Modernizr作为开发HTML5必要的js工具************ Modernizr作为开发HTML5必要的js工具 *********字体大小设 rem********* ...

  4. 安卓学习笔记---Activity

    由于学期实训的要求,我开始学习安卓了.从本月一号开始,学了五天了.时间短,刚学到的东西容易忘,我记一下笔记. 首先是对Activity的理解.activity首先是一个java类,我们创建一个新的ac ...

  5. prelaod场景,用来显示资源加载进度

    phaser.js的源码可以到它在github上的托管里去下载,游戏要用到的图片声音等素材资源请点击这里下载.Phaser的使用非常简单,只需要引入它的主文件,然后在页面中指定一个用来放置canvas ...

  6. ajax利用json进行服务器与客户端的通信

    1.JQuery中$.ajax()方法参数详解 http://blog.sina.com.cn/s/blog_4f925fc30100la36.html 2.服务器端获取String que=requ ...

  7. JSP编程-步步为营

    [第一个JSP举例] header.jsp <%@ page language="java" contentType="text/html; charset=utf ...

  8. 前端编码风格规范(3)—— JavaScript 规范

    JavaScript 规范 全局命名空间污染与 IIFE 总是将代码包裹成一个 IIFE(Immediately-Invoked Function Expression),用以创建独立隔绝的定义域.这 ...

  9. Javascript 判断一个数字是否含有小数点

    JavaScript 判断一个数字是否含有小数点,如果含有,则返回该数字:如果不含小数点,则小数点后保留两位有效数字: function hasDot(num){ if(!isNaN(num)){ r ...

  10. Javascript 笔记与总结(2-3)Javascript 运算符、控制结构与对象操作

    [连接运算符 + ] <script> console.log(1+2+'a'+3+4); </script> 输出: 3a34 [逻辑运算符]返回的是最早能判断表达式结果的那 ...