leetcode Edit Distance
来源:互联网 发布:查看矢量图的软件 编辑:程序博客网 时间:2024/06/14 17:49
Description:
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:
状态转移方程如下
1. d[0, j] = j;
2. d[i, 0] = i;
3. d[i, j] = d[i-1, j - 1] if A[i] == B[j]
4. d[i, j] = min(d[i-1, j - 1], d[i, j - 1], d[i-1, j]) + 1 if A[i] != B[j]
class Solution {public: int minDistance(string word1, string word2) { int m = word1.size(); int n = word2.size(); vector<vector<int>> res(m + 1, vector<int>(n + 1, 0)); for (int i = 0; i <= m; i++) res[i][0] = i; for (int i = 0; i <= n; i++) res[0][i] = i; for (int i = 1; i <= m; i++) { for (int j = 1; j <= n; j++) { if (word1[i - 1] == word2[j - 1]) { res[i][j] = res[i-1][j-1]; } else { res[i][j] = min(res[i-1][j], min(res[i][j-1], res[i-1][j-1])) + 1; } } } return res[m][n]; }};
阅读全文
0 0
- LeetCode: Edit Distance
- LeetCode Edit Distance
- LeetCode: Edit Distance
- [Leetcode] Edit Distance
- leetcode 19: Edit Distance
- [LeetCode] Edit Distance
- [Leetcode] Edit Distance
- LeetCode Edit Distance
- [LeetCode] Edit Distance
- [LeetCode]Edit Distance
- Leetcode: Edit Distance
- [leetcode]Edit Distance
- LeetCode-Edit Distance
- [leetcode] Edit Distance
- LeetCode - Edit Distance
- leetcode之Edit Distance
- leetcode edit distance
- 【leetcode】Edit Distance
- python高级1:面向对象进阶
- AfxGetModuleState() 与 AfxGetModuleThreadState()
- 【亲测有效】Windows下安装Python的wordcloud
- 回到起点,重新出发
- Android 修改状态栏和标题栏颜色
- leetcode Edit Distance
- 计算机原理图,pcb
- 回流与重绘
- mysql 索引笔记
- SPOJ PROOT Primitive Root (数学找规律)
- coursera-斯坦福-机器学习-吴恩达-第5周笔记-反向传播
- 泛型集合框架
- eclipse中插件修改svn用户名密码
- 结构体初始化问题——引入未知参数