【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: [ [], [], [], [,,], [,], ...
随机推荐
- HTML5+学习笔记1-------边看代码边研究中
document.addEventListener('touchstart',function(){ return false; },true); touchstart当手指触摸屏幕时候触发,即使已经 ...
- 密码学初级教程(八)SSL/TLS-为了更安全的通信
SSL/TLS是世界上应用最广泛的密码通信方法.用到对称密码.消息认证码.公钥密码.数字签名.伪随机数生成器等密码技术. 密码套件 SSL/TLS提供了一种密码通信的框架,SSL/TLS中使用的对称密 ...
- PHP函数preg_replace() 正则替换所有符合条件的字符串
PHP preg_replace() 正则替换,与JavaScript 正则替换不同,PHP preg_replace() 默认就是替换所有符号匹配条件的元素. preg_replace (正则表达式 ...
- APPCAN MAS接口之AJAX
1.打开APPCAN IDE,文件→新建→MAS服务 2.新建MAS项目 3.新建MAS接口,访问地址http://mobile.163.com/special/chuangye/ 4.修改if_cy ...
- PYTHONPATH 可以跨版本 方便使用 (本文为windows方法)转~
PYTHONPATH是Python搜索路径,默认我们import的模块都会从PYTHONPATH里面寻找. 使用下面的代码可以打印PYTHONPATH: print(os.sys.path) 我的某个 ...
- centos-6.5 安装apache
1.避免端口.程序冲突.使用rpm卸载httpd [root@www /]# rpm -qa httpd #检查httpd的包 [root@www /]# rpm -e httpd --nodeps ...
- Ping CAP CTO、Codis作者谈redis分布式解决方案和分布式KV存储
此文根据[QCON高可用架构群]分享内容,由群内[编辑组]志愿整理,转发请注明出处. 苏东旭,Ping CAP CTO,Codis作者 开源项目Codis的co-author黄东旭,之前在豌豆荚从事i ...
- vncserver和Ubuntu Xfce4远程桌面环境的配置,解决不显示图形界面
vncserver和Ubuntu Xfce4远程桌面环境的配置 参考的http://blog.163.com/thinki_cao/blog/static/8394487520130301453180 ...
- [KOJ6024]合并果子·改(强化版)
[COJ6024]合并果子·改(强化版) 试题描述 在一个果园里,多多已经将所有的果子打了下来,而且按果子的不同种类分成了不同的堆.多多把这些果子堆排成一排,然后所有的果子合成一堆. 每一次合并 ...
- SSH-Struts第四弹:Struts2学习过程中遇到的问题
1.2014-03-27 22:03:10 问题点:在struts.xml中进行常量的配置:devMode=true不起作用,还是必须重新启动tomcat,才能使修改过的struts.xml和Acti ...