[LeetCode] Edit Distance(很好的DP)
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)的更多相关文章
- [LeetCode] Edit Distance 编辑距离
Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2 ...
- Leetcode:Edit Distance 解题报告
Edit Distance Given two words word1 and word2, find the minimum number of steps required to convert ...
- [leetcode]Edit Distance @ Python
原题地址:https://oj.leetcode.com/problems/edit-distance/ 题意: Given two words word1 and word2, find the m ...
- LeetCode: Edit Distance && 子序列题集
Title: Given two words word1 and word2, find the minimum number of steps required to convert word1 t ...
- 编辑距离Edit Distance 非常典型的DP类型题目
https://leetcode.com/problems/edit-distance/?tab=Description 真的非常好,也非常典型. https://discuss.leetcode.c ...
- Leetcode Edit Distance
Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2 ...
- LeetCode——Edit Distance
Question Given two words word1 and word2, find the minimum number of steps required to convert word1 ...
- [LeetCode] Edit Distance 字符串变换为另一字符串动态规划
Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2 ...
- Leetcode之动态规划(DP)专题-72. 编辑距离(Edit Distance)
Leetcode之动态规划(DP)专题-72. 编辑距离(Edit Distance) 给定两个单词 word1 和 word2,计算出将 word1 转换成 word2 所使用的最少操作数 . 你可 ...
随机推荐
- LightOJ1086 Jogging Trails(欧拉回路+中国邮递员问题+SPFA)
题目求从某点出发回到该点经过所有边至少一次的最短行程. 这个问题我在<图论算法理论.实现及应用>中看过,是一个经典的问题——中国邮递员问题(CPP, chinese postman pro ...
- python 代码片段
print 'dongshen' for word in ['capitalize','these','words']: print word.upper() for i in range(0,5): ...
- cocos2d CCArray
CCArray* arr=CCArray::create(); arr->retain();//如果不加这个东西,CCArray会被清空 arr->addObject(CCSprite:: ...
- Add Customerlize Button in More Button List In Odoo
There're two commen type of actions in odoo: ir.actions.server,ir.actions.client_multi 1.Using ir.ac ...
- JS - 超强大文本动画插件Textillate.js
http://www.yyyweb.com/demo/textillate/ Textillate.js AsimplepluginforCSS3textanimations.
- Wilddog - 野狗统计
根据业务需求提出的统计代码. <!DOCTYPE HTML> <html lang="en-US"> <head> <meta chars ...
- MemPool
腾讯笔试题,设计内存池,alloc和free都是O(1). 和LRUCache类似,这里用了一个list表示可用的空间,用一个map来记录这块内存是否已分配,这样free的时候才可能O(1). cla ...
- 启动Automatic Updates出现0x80004015错误的解决办法
前几天我的本本加入到AD里面了,并且换了个用户名,结果昨天就发现升级出毛病了,Automatic Updates服务无法启动,启动时候出现0x80004015错误:Automatic Updates ...
- 清橙 A1206 小Z的袜子(莫队算法)
A1206. 小Z的袜子 时间限制:1.0s 内存限制:512.0MB 总提交次数:1357 AC次数:406 平均分:46.75 将本题分享到: 查看未格式化的试题 ...
- HTML5:离线存储(缓存机制)-IndexDB
https://www.w3.org/TR/IndexedDB/ .. <!DOCTYPE html> <html> <head> <meta charset ...