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
输入两个单词word1和word2,求出从word1转换成word2的最少步骤。每个转换操作算一步。转换操作限定于:
- 删除一个字符
- 插入一个字符
- 替换一个字符
本题用动态规划求解
设决策变量dp[i][j],表示从word[0..i-1]转换为word2[0...j-1]的最少步骤
可以参考http://web.stanford.edu/class/cs124/lec/med.pdf
class Solution {
public:
int minDistance(string word1, string word2) {
int len1 =word1.length(), len2 = word2.length();
if(len1 == ) return len2;
if(len2 == ) return len1;
if(word1 == word2) return ;
vector<vector<int> > dp(len1+, vector<int>(len2+,));
for(int i = ; i <= len1; ++ i) dp[i][] = i;
for(int j = ; j <= len2; ++ j) dp[][j] = j;
for(int i =; i <= len1; ++ i){
for(int j = ; j <= len2; ++ j){
dp[i][j] = min(dp[i-][j-]+(word1[i-] != word2[j-]? : ),min(dp[i-][j]+, dp[i][j-]+));
}
}
return dp[len1][len2];
}
};
Leetcode Edit Distance的更多相关文章
- [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 字符串变换为另一字符串动态规划
Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2 ...
- [LeetCode] Edit Distance(很好的DP)
Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2 ...
- LeetCode: Edit Distance && 子序列题集
Title: Given two words word1 and word2, find the minimum number of steps required to convert word1 t ...
- LeetCode——Edit Distance
Question Given two words word1 and word2, find the minimum number of steps required to convert word1 ...
- [LeetCode] One Edit Distance 一个编辑距离
Given two strings S and T, determine if they are both one edit distance apart. 这道题是之前那道Edit Distance ...
- 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 ...
随机推荐
- 好用的开源web系统总结
1.论坛 phpwind 一个用wind框架写的论坛 discuz 社区动力 论坛 2.商城 Ecshop 商城腾讯的开源商城项目 一款B2C独立网店系统,系统是基于PHP语言及MYS ...
- AE开发实现GP工具IDW
IDW——空间插值 IDW(Inverse Distance Weighted)是一种常用而简便的空间插值方法,它以插值点与样本点间的距离为权重进行加权平均,离插值点越近的样本点赋予的权重越大. 设平 ...
- 常见input输入框 点击 发光白色外阴影 focus
先看看具体实现的效果 第一就是点击input 实现的效果 默认谷歌点击input是蓝色边框 去掉用outline:0; 实现效果用focus 默认状态的边框颜色一般较重 如border:1px s ...
- mysql优化记录
老板反应项目的反应越来越慢,叫优化一下,顺便学习总结一下mysql优化. 不同引擎的优化,myisam读的效果好,写的效率差,使用场景 非事务型应用只读类应用空间类应用 Innodb的特性,innod ...
- linux用命令删除重复行
文本处理时,经常要删除重复行,下面是三种方法 第一,用sort+uniq,注意,单纯uniq是不行的. sort -n test.txt | uniq 第二,用sort+awk命令,注意,单纯awk同 ...
- VTK初学一,比较常见的错误1
错误原因: 通常是在文件头部没有初始化 #ifndef INITIAL_OPENGL #define INITIAL_OPENGL #include <vtkAutoInit.h> V ...
- Coursera-Getting and Cleaning Data-week4-R语言中的正则表达式以及文本处理
博客总目录:http://www.cnblogs.com/weibaar/p/4507801.html Thursday, January 29, 2015 补上第四周笔记,以及本次课程总结. 第四周 ...
- flash_header.S ( freescale imx6 board)
/* * Copyright (C) 2011-2012 Freescale Semiconductor, Inc. * * This program is free software; you ca ...
- 子类可以有跟父类中同名的方法,但是会重写父类中的方法,甚至是root class中的方法
/* 子类可以重写父类中的方法,甚至是root class中的方法,比如NSObeject 的new方法,但是后提示警告如下 Method is expected to return an insta ...
- css3的基本样式
一.css3字体(@font-face) 二.css3动画 三.css3多列 四.CSS3 outline-offset 属性 五.CSS3 resize 属性