【leetcode】Edit Distance (hard)
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
思路:
这个去年学算法的时候学过,当时觉得好难啊,现在一看,这题真简单。就是一道常规的动态规划题目。直接AC,高兴~~
用dp[m][n]存储word1的前m个字符与word2的前n个字符的最小匹配距离。
那么dp[i][j] = dp[i-1][j]+1 (word1删除一个字符)、dp[i][j-1] + 1 (word2删除一个字符)、 dp[i-1][j-1] + ((word1[i-1]==word2[j-1]) ? 0(当前字符相等) : 1(替换))) 中最小的值。
class Solution {
public:
int minDistance(string word1, string word2) {
int len1 = word1.length();
int len2 = word2.length();
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(min(dp[i-][j] + , dp[i][j-] + ), dp[i-][j-] + ((word1[i-]==word2[j-]) ? : ));
}
}
return dp[len1][len2];
}
};
【leetcode】Edit Distance (hard)的更多相关文章
- 【leetcode】Edit Distance
Edit Distance Given two words word1 and word2, find the minimum number of steps required to convert ...
- 【LeetCode】【动态规划】Edit Distance
描述 Given two words word1 and word2, find the minimum number of operations required to convert word1 ...
- 【LeetCode】Hamming Distance
问题网址 https://leetcode.com/problems/hamming-distance/ 就是一个异或后,求1的位数的问题. 看到问题之后,首先困扰是: int能不能求异或?是不是要转 ...
- 【leetcode】1184. Distance Between Bus Stops
题目如下: A bus has n stops numbered from 0 to n - 1 that form a circle. We know the distance between al ...
- 【LeetCode】字符串 string(共112题)
[3]Longest Substring Without Repeating Characters (2019年1月22日,复习) [5]Longest Palindromic Substring ( ...
- 【LeetCode】863. All Nodes Distance K in Binary Tree 解题报告(Python)
[LeetCode]863. All Nodes Distance K in Binary Tree 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http ...
- 【LeetCode】849. Maximize Distance to Closest Person 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【LeetCode】二叉查找树 binary search tree(共14题)
链接:https://leetcode.com/tag/binary-search-tree/ [220]Contains Duplicate III (2019年4月20日) (好题) Given ...
- 【LeetCode】位运算 bit manipulation(共32题)
[78]Subsets 给了一个 distinct 的数组,返回它所有的子集. Example: Input: nums = [,,] Output: [ [], [], [], [,,], [,], ...
随机推荐
- WCF 已知类型和泛型解析程序 KnownType
数据协定继承 已知类型和泛型解析程序 Juval Lowy 下载代码示例 自首次发布以来,Windows Communication Foundation (WCF) 开发人员便必须处理数据协定继承方 ...
- mysql 总结二(自定义函数)
本质:mysql内置函数的一种扩展,本质上与mysql内置函数一样. 函数必要条件: @1:参数(非必备): @2:返回值: 模板: create function function_name ret ...
- 【Tomcat】tomcat报错 removeGeneratedClassFiles failed
程序放到测试环境一点问题没有,放到正式环境都是问题.总感觉是环境的问题,环境能带来问题,但是不是所有问题都能说是环境带来的. 这点,要改正.还要淡定对待问题.看错误. 程序是不会骗你的.这个问题折磨了 ...
- webrtc第二篇 聊天室
聊天室模型不一样考虑的问题也不一样 1.websocket文本聊天 * step1 : 向聊天室所有用户(不包括该用户自己)发送当前用户上线信息.客户端用户栏回添加此用户 * step2 : 将该用户 ...
- phpcms标签云
{pc:get sql="SELECT keyword FROM v9_keyword WHERE siteid=$siteid AND searchnums > 5 ORDER BY ...
- tableview侧滑删除
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath { ) { retu ...
- H5canvas赛车游戏-基于lufylegend引擎
lufylegend引擎是canvas游戏中,比较简单的引擎之一,它不需要配置环境,类似引入jquery包的方式,引用对应js文件即可 lufylegend官方网站:http://www.lufyle ...
- buildroot 制作Linux文件系统初级使用教程
buildroot 下载地址:https://buildroot.org/download.html 放在Linux文件下解压出来. 使用make menuconfig 进行配置相关的东西. 在使用这 ...
- Vijos 1055 奶牛浴场
Description 求一个不覆盖指定点的最大子矩阵,\(n,m \leqslant 3\times 10^5,S \leqslant 5\times 10^3\) . Sol 没有名字的算法都叫x ...
- js之序列化、eval和Date类用法
序列化 JSON.stringify() 将对象转换为字符串 JSON.parse() 将字符串转换为对象类型 示例 var jsonStr = '{"name":"le ...