Question

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

Solution

动态规划。参见以前的博客:http://www.cnblogs.com/zhonghuasong/p/6147030.html

Code

class Solution {
public:
int minDistance(string word1, string word2) {
int len1 = word1.length();
int len2 = word2.length();
vector<vector<int>> dp(len1 + 1, vector<int>(len2 + 1, 0));
for (int i = 0; i <= len1; i++)
dp[i][0] = i;
for (int j = 0; j <= len2; j++)
dp[0][j] = j;
for (int i = 1; i <= len1; i++) {
for (int j = 1; j <= len2; j++) {
int tmp = word1[i - 1] == word2[j - 1] ? 0 : 1;
dp[i][j] = min(dp[i - 1][j] + 1, min(dp[i][j - 1] + 1, dp[i - 1][j - 1] + tmp));
}
}
return dp[len1][len2];
}
};

LeetCode——Edit Distance的更多相关文章

  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 字符串变换为另一字符串动态规划

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

  5. Leetcode Edit Distance

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

  6. [LeetCode] Edit Distance(很好的DP)

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

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

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

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

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

  9. 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 ...

随机推荐

  1. UIScreen(屏幕)、UIWindow(画框)、UIView(画布)、didFinishLaunchingWithOptions的概念

    //didFinishLaunchingWithOptions 方法:顾名思义.在app开始运行时会调用里面的方法.- (BOOL)application:(UIApplication *)appli ...

  2. 苏宁易购微信端 全页通过background单图

    w单图,绕开了显示的兼容性. http://res.m.suning.com/project/JoinGo/intro.html http://res.m.suning.com/project/Joi ...

  3. trust an HTTPS connection

    https://zh.wikipedia.org/wiki/传输安全协议 SSL协议客户端要收发几个握手信号: 发送一个“ClientHello”消息,内容包括:支持的协议版本,比如TLS1.0版,一 ...

  4. 网络模型一般是指 OSI 七层参考模型和 TCP/IP 五层参考模型。

    网络模型一般是指 OSI 七层参考模型和 TCP/IP 五层参考模型. 每一层实现各自的功能和协议,并且都为上一层提供业务功能.为了提供这 种业务功能,下一层将上一层中的数据并入到本层的数据域中,然后 ...

  5. Intellij idea中maven加载jar包很慢的解决方案.

    默认加载的都是国外的源,我们可以配置国内的源. 右键项目-->maven-->Open ''setting.xml'' 复制下面的代码进去.保存. 我这里使用的版本是 ideaIU-14. ...

  6. 如何缩减手游app安装包的大小?

    包体过大对手游的影响更是诟病已久,有具体数据证明,游戏包体越大,在游戏运营推广过程中游戏用户的转化率就越低:反之,游戏包体越小,游戏用户的下载转化率就越高(如下图),所有的手机app.游戏在大版本更新 ...

  7. golang处理 json中非法字符

    原文: Hi there, I just discovered Go and decided to port a little program to Go. The program reads JSO ...

  8. Linux cd命令 pwd命令

    1.cd命令 cd:及Change Directory改变目录的意思,用于更改到指定的目录 用法:cd [目录] 其中 "."代表当前目录,".."代表当前目录 ...

  9. wpa安装方法

    1.openssl 2.lib 1.1.2 3.wpa lua 编译错误 http://www.blogjava.net/xiaomage234/archive/2013/09/13/404037.h ...

  10. 在PL/SQL中如何让程序暂停几秒钟

    在编写PL/SQL中,有时需要程序中暂停几秒钟再继续执行,查了一下,oracle内置有这个功能dbms_lock.sleep(10):不过dbms_lock包需要用户自己安装,演示如下: C:\Doc ...