描述

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

You have the following 3 operations permitted on a word:

  1. Insert a character
  2. Delete a character
  3. Replace a character

Example 1:

Input: word1 = "horse", word2 = "ros"
Output: 3
Explanation:
horse -> rorse (replace 'h' with 'r')
rorse -> rose (remove 'r')
rose -> ros (remove 'e')

Example 2:

Input: word1 = "intention", word2 = "execution"
Output: 5
Explanation:
intention -> inention (remove 't')
inention -> enention (replace 'i' with 'e')
enention -> exention (replace 'n' with 'x')
exention -> exection (replace 'n' with 'c')
exection -> execution (insert 'u')

思路:动态规划

这是一个经典的动态规划问题,思路参考斯坦福的课程:http://www.stanford.edu/class/cs124/lec/med.pdf

这里把加2变成加1即可

  1. dp[i][0] = i;
  2. dp[0][j] = j;
  3. dp[i][j] = dp[i - 1][j - 1], if word1[i - 1] = word2[j - 1];
  4. dp[i][j] = min(dp[i - 1][j - 1] + 1, dp[i - 1][j] + 1, dp[i][j - 1] + 1), otherwise.
class Solution {
public:
int minDistance(string word1, string word2) {
int m = word1.size(), n = word2.size();
vector<vector<int> > dp(m+, vector<int>(n+, ));
for(int i = ;i<=m;++i)
dp[i][] = i;
for(int i = ;i<=n;++i)
dp[][i] = i;
for(int i = ;i<=m;++i){
for(int j = ;j<=n;++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])) + ;
}
}
return dp[m][n];
}
};

【LeetCode】【动态规划】Edit Distance的更多相关文章

  1. [Leetcode Week8]Edit Distance

    Edit Distance 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/edit-distance/description/ Description ...

  2. [LeetCode] 72. Edit Distance 编辑距离

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

  3. [LeetCode] One Edit Distance 一个编辑距离

    Given two strings S and T, determine if they are both one edit distance apart. 这道题是之前那道Edit Distance ...

  4. 【leetcode】Edit Distance

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

  5. Java for LeetCode 072 Edit Distance【HARD】

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

  6. LeetCode One Edit Distance

    原题链接在这里:https://leetcode.com/problems/one-edit-distance/ Given two strings S and T, determine if the ...

  7. leetCode 72.Edit Distance (编辑距离) 解题思路和方法

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

  8. [LeetCode] 72. Edit Distance(最短编辑距离)

    传送门 Description Given two words word1 and word2, find the minimum number of steps required to conver ...

  9. LeetCode - 72. Edit Distance

    最小编辑距离,动态规划经典题. Given two words word1 and word2, find the minimum number of steps required to conver ...

  10. 【leetcode】Edit Distance (hard)

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

随机推荐

  1. IRQ与FIQ的区别

    1.对FIQ你必须进快处理中断请求,并离开这个模式. 2.IRQ可以被FIQ所中断,但FIQ不能被IRQ所中断,在处理FIQ时必须要关闭中断. 3.FIQ的优先级比IRQ高. 4.FIQ模式下,比IR ...

  2. error: xslt-config not found. Please reinstall the libxslt >= 1.1.0 distribution

    解决编译php扩展xsl时出现 error: xslt-config not found. Please reinstall the libxslt >= 1.1.0 distribution ...

  3. Git使用技巧(3)-- 远程操作

    Git远程操作详解 作者: 阮一峰 编辑更新:shifu204 日期: 2016年9月1日 Git是目前最流行的版本管理系统,学会Git几乎成了开发者的必备技能. Git有很多优势,其中之一就是远程操 ...

  4. MFC多国语言——配置文件

    前段时间,因工作需要,本地化了一个英文版本的产品. 在网上查阅了若干资料,在此进行一个简单的整理. 在MFC程序中,实现多国语言的方式很多,我们选择的是使用配置文件的方法. 在通过配置文件方式实现多国 ...

  5. iOS - 逆向 - Objective-C代码混淆 -confuse.sh文件写法

    class-dump可以很方便的导出程序头文件,不仅让攻击者了解了程序结构方便逆向,还让着急赶进度时写出的欠完善的程序给同行留下笑柄. 所以,我们迫切的希望混淆自己的代码. 混淆的常规思路 混淆分许多 ...

  6. python:编写登陆接口(day 1)

    作业要求: 输入用户名,密码 认证成功显示欢迎信息 输入错误三次后锁定用户 Readme 1.user_id.txt是存放用户id及密码的文件 2.user_lock.txt是存放被锁定的用户id的文 ...

  7. How to convert BigDecimal to Double in spring-data-mongodb framework

    问题描述:我们都知道对于涉及钱的数据必须使用BigDecimal类型进行存储,今天在查询mongo时仍然有精度问题,虽然我在代码中使用了Big Decimal类型,但mongo中使用的是double类 ...

  8. java编程:将数组的第一个为最大第二个为最小以此类推

    import java.util.Scanner; public class Max_Min { public static void main(String[] args) { int[] a = ...

  9. 【BZOJ3122】[Sdoi2013]随机数生成器 BSGS+exgcd+特判

    [BZOJ3122][Sdoi2013]随机数生成器 Description Input 输入含有多组数据,第一行一个正整数T,表示这个测试点内的数据组数.   接下来T行,每行有五个整数p,a,b, ...

  10. Introduction to Mathematical Thinking - Week 6 - Proofs with Quantifieers

    Mthod of proof by cases 证明完所有的条件分支,然后得出结论. 证明任意 使用任意 注意,对于一个任意的东西,你不知道它的具体信息.比如对于任意正数,你不知道它是 1 还是 2等 ...