【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: [ [], [], [], [,,], [,], ...
随机推荐
- etcd
https://github.com/silenceper/dcmp http://studygolang.com/topics/1866
- Ubuntu使用ApkTool进行APK反编译
1.Apktool下载 http://ibotpeaches.github.io/Apktool/ 下载最新版本Apktool_2.1.1.jar 2.新建一个apktool目录,将Apktool_2 ...
- Oracle锁的机制
一.为什么要有锁的机制 我们都知道数据库是一个多用户使用的共享资源.当多个用户并发地存取数据时,在数据库中就会产生多个事务同时存取同一数据的情况.若对并发操作不加控制就可能会读取和存储不正确的数据,破 ...
- C++ 模拟虚拟键盘按键表
键盘VK键值列表 /* Virtual Keys, Standard Set*/ VK_LBUTTON 0x01 VK_RBU ...
- C#高级知识点01---委托和事件
委托和事件 什么是委托? 简单来说,就是能把方法当作参数传递的对象,而且还知道怎么去调用这个方法,同时还约束了方法的签名. 例子: 用委托实现插件式编程: 1.
- 《Lua程序设计 第二版》学习笔记一
Lua简介 Lua是一种简单.可拓展.可移植及高效的脚本语言. 开始 Lua之间不需要分隔符 运行方式: Linux下: lua -i prog dofile("lib1.lua" ...
- JQuery simpleModal插件的使用-遁地龙卷风
(0)写在前面 jquery.simpleModal.浏览器这三者的兼容性,不仅显示在报错上,还体现在所呈现的效果不是预期上. 说一下我的环境 jquery-1.8.3.js jquery.simpl ...
- Android学习笔记(十三)——广播机制
//此系列博文是<第一行Android代码>的学习笔记,如有错漏,欢迎指正! Android 中的每个应用程序都可以对自己感兴趣的广播进行注册,这样该程序就只会接收到自己所关心的广播内容 ...
- [POJ1151]Atlantis
[POJ1151]Atlantis 试题描述 There are several ancient Greek texts that contain descriptions of the fabled ...
- [codeforces 339]C. Xenia and Weights
[codeforces 339]C. Xenia and Weights 试题描述 Xenia has a set of weights and pan scales. Each weight has ...