题目

给定两个单词 word1 和 word2,计算出将 word1 转换成 word2 所使用的最少操作数 。

你可以对一个单词进行如下三种操作:

插入一个字符

删除一个字符

替换一个字符

思路

定义一个数组dp[i][j]代表第一个字符串前i个字符转换为第二个字符串前j个字符串所需要的缩少操作数。

如果word1[1] == word2[j],那么dp[i][j] = dp[i - 1][ j - 1]

不然呢,执行三个操作,分别对应dp[i - 1][j - 1] + 1dp[i - 1][j] + 1dp[i][j + 1]

本题中,需要额外注意数组的初始化、以及数组的大小。

class Solution {
public:
int minDistance(string word1, string word2) {
//init
long long dp[word1.size() + 1][word2.size() + 1];
memset(dp, 0, sizeof(dp));
for(int i = 0; i <= word1.size(); i++){
dp[i][0] = i;
}
for(int i = 0; i <= word2.size(); i++){
dp[0][i] = i;
} //dp
for(int i = 1; i <= word1.size(); i++){
for(int j = 1; j <= word2.size(); j++){
if(word1[i- 1] == word2[j - 1]) dp[i][j] = dp[i - 1][j - 1];
else dp[i][j] = min(dp[i - 1][j - 1], min(dp[i - 1][j], dp[i][j - 1])) + 1;
}
}
return dp[word1.size()][word2.size()];
}
};

LeetCode 编辑距离(DP)的更多相关文章

  1. HDU 4323 Magic Number(编辑距离DP)

    http://acm.hdu.edu.cn/showproblem.php?pid=4323 题意: 给出n个串和m次询问,每个询问给出一个串和改变次数上限,在不超过这个上限的情况下,n个串中有多少个 ...

  2. [LeetCode]72. 编辑距离(DP)

    题目 给定两个单词 word1 和 word2,计算出将 word1 转换成 word2 所使用的最少操作数 . 你可以对一个单词进行如下三种操作: 插入一个字符 删除一个字符 替换一个字符 示例 1 ...

  3. 51nod 1183 编辑距离(dp)

    题目链接:51nod 1183 编辑距离 #include<cstdio> #include<cstring> #include<algorithm> using ...

  4. leetcode distinct-subsequences(DP)

    参考https://oj.leetcode.com/problems/distinct-subsequences 动态规划方程 dp[i][j]=dp[i-1][j-1]+dp[i-1][j] (s( ...

  5. Codeforces 67C Sequence of Balls 编辑距离 dp

    题目链接:点击打开链接 有一个交换操作比較特殊,所以记录每一个点距离自己近期的那个字符的位置 然后交换就相当于把第一行要交换的2个字符 之间的字符都删掉 把第二行要交换的2个字符 之间的字符都插入第一 ...

  6. [Leetcode][Python][DP]Regular Expression Matching

    # -*- coding: utf8 -*-'''https://oj.leetcode.com/problems/regular-expression-matching/ Implement reg ...

  7. leetcode 编辑距离

    class Solution { public: int minDistance(string word1, string word2) { // Start typing your C/C++ so ...

  8. leetcode HouseRobber Dp Code

    #include <malloc.h> int MAX(int x,int y){ return x>y?x:y;} int rob(int* nums, int numsSize) ...

  9. Leetcode:【DP】Longest Palindromic Substring 解题报告

    Longest Palindromic Substring -- HARD 级别 Question SolutionGiven a string S, find the longest palindr ...

随机推荐

  1. date-fns时间库的基本使用

    在react中使用date-fns: import sub_days from 'date-fns/sub_days'; import start_of_week from 'date-fns/sta ...

  2. 1.spring异常:Caused by: java.lang.NoClassDefFoundError: org/aopalliance/intercept/MethodInterceptor

    org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springfr ...

  3. 【Python】循环的拓展

  4. 训练20191007 2017-2018 ACM-ICPC Latin American Regional Programming Contest

    2017-2018 ACM-ICPC Latin American Regional Programming Contest 试题地址:http://codeforces.com/gym/101889 ...

  5. C语言-数组指针与指针数组

    1.思考 下面这些声明合法吗? int array[5]; int matrix[3][3]; int * pa = array; int * pm = matrix; 问题: array代表数组首元 ...

  6. 计数器IP核

    Quartus II提供的LPM_couter IP核的使用 FPGA设计方式: 原理图,Verilog HDL设计方式,IP核输入方式 创建IP核 点击TOOLS—IP catalog-libray ...

  7. 1.Java多线程之wait和notify

    1.首先我们来从概念上理解一下这两个方法: (1)obj.wait(),当obj对象调用wait方法时,这个方法会让当前执行了这条语句的线程处于等待状态(或者说阻塞状态),并释放调用wait方法的对象 ...

  8. Flink架构(二)- Flink中的数据传输

    2. Flink中的数据传输 在一个运行的application中,它的tasks在持续交换数据.TaskManager负责做数据传输.TaskManager的网络组件首先从缓冲buffer中收集re ...

  9. MyEcplise中编码格式的修改问题

    1.如果是在Run Configurations中修改编码格式的话,只能是修改当前java文件的编码格式,把改文件中的代码复制到 另一新建 的java文件中会出现异常,所以就会出现相同的代码在两个不同 ...

  10. 每天进步一点点------创建Microblaze软核(一)

    在使用FPGA时,有时会用到它做为主控芯片.对于习惯于单片机及C语言开发的人,使用FPGA做主控芯片,首先还是想到它的嵌入式软核功能.如果能够基于Microblze软核进行C语言程序的开发,相对于使用 ...