LeetCode_Edit Distance
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
class Solution {
public:
int minDistance(string word1, string word2) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
int m = word1.size();
int n = word2.size();
vector<vector<int>> table(m+1,vector<int>(n+1, 0));
for(int i = 1; i <= n; i++) table[0][i] = i;
for(int i = 1; i <= m; i++) table[i][0] = i;
for(int i = 1; i <= m; i++)
for(int j = 1; j <= n;j++)
{
int min1 = table[i-1][j] < table[i][j-1] ? table[i-1][j] : table[i][j-1];
min1++;
int min2 = word1[i-1] == word2[j-1] ? 0 : 1;
min2 += table[i-1][j-1];
table[i][j] = min1 < min2 ? min1 : min2;
}
return table[m][n];
}
};
reference :http://www.geeksforgeeks.org/dynamic-programming-set-5-edit-distance/
LeetCode_Edit Distance的更多相关文章
- [LeetCode] Total Hamming Distance 全部汉明距离
The Hamming distance between two integers is the number of positions at which the corresponding bits ...
- [LeetCode] Hamming Distance 汉明距离
The Hamming distance between two integers is the number of positions at which the corresponding bits ...
- [LeetCode] Rearrange String k Distance Apart 按距离为k隔离重排字符串
Given a non-empty string str and an integer k, rearrange the string such that the same characters ar ...
- [LeetCode] Shortest Distance from All Buildings 建筑物的最短距离
You want to build a house on an empty land which reaches all buildings in the shortest amount of dis ...
- [LeetCode] Shortest Word Distance III 最短单词距离之三
This is a follow up of Shortest Word Distance. The only difference is now word1 could be the same as ...
- [LeetCode] Shortest Word Distance II 最短单词距离之二
This is a follow up of Shortest Word Distance. The only difference is now you are given the list of ...
- [LeetCode] Shortest Word Distance 最短单词距离
Given a list of words and two words word1 and word2, return the shortest distance between these two ...
- [LeetCode] One Edit Distance 一个编辑距离
Given two strings S and T, determine if they are both one edit distance apart. 这道题是之前那道Edit Distance ...
- [LeetCode] Edit Distance 编辑距离
Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2 ...
随机推荐
- .net连接oracle(无客户端)
使用DDTek.Oracle.dll可以在没有安装oracle客户端的情况下连接远程的oracle. string _conString = "Host=192.168.1.1;Port=1 ...
- 关于java中private构造函数的问题
首先推荐以下下面的一篇博客: http://blog.csdn.net/my_dream_fly/article/details/3857887 本来也比较好奇,自己写构造函数总是定义public形式 ...
- iOS 时区问题总结 NSTimeZone
基本概念 GMT 0:00 格林威治标准时间; UTC +00:00 校准的全球时间; CCD +08:00 中国标准时间 [来自百度百科] 夏时制,英文"DaylightSavingTim ...
- Label 添加表情图片
// 添加表情 NSTextAttachment *attch = [[NSTextAttachment alloc] init]; // 表情图片 attch.image = [UIImage im ...
- Android事件处理之多点触摸与手势识别
一.Muilti-touch 双指缩放的实现探索: 首先要实现OnTouchListener接口,然后重写方法: public boolean onTouch(View v, MotionEvent ...
- Java基础知识强化38:StringBuffer类之StringBuffer的添加功能
1. StringBuffer的添加功能: public StringBuffer append(String str):可以把任意类型数据添加到字符串缓冲区里面,并返回字符串缓冲区本身. publ ...
- mysql 的密码重置
Windows: 1.以系统管理员登陆: 2.停止MySQL服务: 3.进入CMD,进入MySQL的安装目录,假设是D:/MySQL/MySQL Server 5.0/: 4.跳过权限检查启动MySQ ...
- 网页CSS
CSS 样式表,(分三类:内联.内嵌.外部) 1,内联, 直接作于于 元素 例: <p style="font-size:14px;"> 2,内嵌 作用于网页 首先 ...
- Head First HTML与CSS — 布局与定位
1.流(flow)是浏览器在页面上摆放HTML元素所用的方法. 对于块元素,浏览器从上到下沿着元素流逐个显示所遇到的各个元素,会在每个块元素之间加一个换行: 对于内联元素,在水平方向会相互挨着,总体上 ...
- 如何学习javascript?(转)
推荐几本好书: Step 1: <JavaScript DOM编程艺术> 看这本书之前,请先确认您对Javascript有个基本的了解,应该知道if else之类的语法,如果不懂,先去看看 ...