[leetcode]72. Edit Distance 最少编辑步数
Given two words word1 and word2, find the minimum number of operations required to convert word1 to word2.
You have the following 3 operations permitted on a word:
- Insert a character
- Delete a character
- Replace a character
题意:
给定两个串串: word1, word2; 找出word1变身成word2的最小步数的操作(仅限插入、删除和替换这三种操作)
思路:
动态规划的高频题,需要熟练掌握。
字符串生成子序列或者字符串的匹配问题,要巴普诺夫条件反射想到dp。
开一个2D array, dp[word1.length() + 1 ][ word2.length() + 1]
word2 = 0 r o s
0 0
word1 = h
o
r
s
e
用dp[i][j]来记录当前word1变身成word2的最小步数
1. 若word1.charAt(i-1) == word2.charAt(j-1) 【留心字符串的index和2D array的坐标有差,这里很容易误写成word1.charAt(i) == word2.charAt(j) 】
dp[i][j] = dp[i-1][j-1]
即若当前word1串串和word2串串的当前字符相同,则不需要做任何convert操作,直接将之前dp[i-1][j-1] 结果拿过来
2. 若word1.charAt(i-1) == word2.charAt(j-1)
dp[i][j] = min( dp[i-1][j-1], dp[i-1][j], dp[i][j-1] ) + 1
即若当前word1串串和word2串串的当前字符不同, 则
要么word1插入 word2当前的字符, dp[i][j] = dp[i][j-1] + 1
要么word1删除 word1当前的字符, dp[i][j] = dp[i-1][j] + 1
要么word1替换 word2当前的字符, dp[i][j] = dp[i-1][j-1] + 1
取以上三个操作中最小的步数
代码:
class Solution {
public int minDistance(String s1, String s2) {
int[][]dp = new int[s2.length() + 1][s1.length() + 1];
dp[0][0] = 0;
for(int i = 1; i<=s2.length(); i++){
dp[i][0] = i;
}
for(int j = 1; j<=s1.length(); j++){
dp[0][j] = j;
}
for(int i = 1; i<=s2.length(); i++){
for(int j = 1; j<=s1.length(); j++){
if( s1.charAt(j-1) == s2.charAt(i-1) ){
dp[i][j] = dp[i-1][j-1];
}else{
dp[i][j] = Math.min(dp[i-1][j-1], Math.min(dp[i][j-1] , dp[i-1][j] )) + 1 ;
}
}
}
return dp[s2.length()][s1.length()];
}
}
[leetcode]72. Edit Distance 最少编辑步数的更多相关文章
- [LeetCode] 72. Edit Distance 编辑距离
Given two words word1 and word2, find the minimum number of operations required to convert word1 to ...
- [LeetCode] 72. Edit Distance(最短编辑距离)
传送门 Description Given two words word1 and word2, find the minimum number of steps required to conver ...
- leetCode 72.Edit Distance (编辑距离) 解题思路和方法
Edit Distance Given two words word1 and word2, find the minimum number of steps required to convert ...
- [leetcode] 72. Edit Distance (hard)
原题 dp 利用二维数组dp[i][j]存储状态: 从字符串A的0~i位子字符串 到 字符串B的0~j位子字符串,最少需要几步.(每一次删增改都算1步) 所以可得边界状态dp[i][0]=i,dp[0 ...
- 第十八周 Leetcode 72. Edit Distance(HARD) O(N^2)DP
Leetcode72 看起来比较棘手的一道题(列DP方程还是要大胆猜想..) DP方程该怎么列呢? dp[i][j]表示字符串a[0....i-1]转化为b[0....j-1]的最少距离 转移方程分三 ...
- LeetCode - 72. Edit Distance
最小编辑距离,动态规划经典题. Given two words word1 and word2, find the minimum number of steps required to conver ...
- 【Leetcode】72 Edit Distance
72. Edit Distance Given two words word1 and word2, find the minimum number of steps required to conv ...
- 刷题72. Edit Distance
一.题目说明 题目72. Edit Distance,计算将word1转换为word2最少需要的操作.操作包含:插入一个字符,删除一个字符,替换一个字符.本题难度为Hard! 二.我的解答 这个题目一 ...
- [Leetcode Week8]Edit Distance
Edit Distance 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/edit-distance/description/ Description ...
随机推荐
- zombodb安装试用
pg 数据库安装 参考如下安装 yum install https://download.postgresql.org/pub/repos/yum/10/redhat/rhel-7-x86_64/pg ...
- 汉语言处理工具pyhanlp的拼音转换与字符正则化
汉字转拼音 HanLP中的汉字转拼音功能也十分的强大. 说明: l HanLP不仅支持基础的汉字转拼音,还支持声母.韵母.音调.音标和输入法首字母首声母功能. l HanLP能够识别多音字,也能给繁体 ...
- 理解java回调机制
摘自:ImportNew 以前不理解什么叫回调,天天听人家说加一个回调方法啥的,心里想我草,什么叫回调方法啊?然后自己就在网上找啊找啊找,找了很多也不是很明白,现在知道了,所谓回调:就是A类中调用B类 ...
- mysql 启动失败,数据恢复
mysql 启动失败,数据恢复 2017年02月13日 16:46:36 阅读数:621 Forcing InnoDB Recovery提供了6个等级的修复模式,需要注意的是值大于3的时候,会对数据文 ...
- ajax请求完成执行的操作
var createAjax = $("#createId").ajax(function(){ //ajax操作 }); $.when(createAjax).done(func ...
- GitExtentions添加SSH证书,pull和push时不必输入密码
1. 工具-->设置,选择SSH,选择PuTTY 2. 选择 3. 在新打开的页面,可以生成key或者加载已有的key.putty的key是.ppk结尾的特殊格式.关于生成key和在githu ...
- Linux系统挂载新磁盘
执行df -h 查看已挂载磁盘 未能找到挂载磁盘 1)使用fdisk -l命令查看主机上的硬盘 红圈标志为后挂载的磁盘未能挂载 2)使用mkfs.ext4命令把硬盘格式化: mkfs.ext4 /de ...
- 【剑指offer】链表第一个公共子结点
*思路: 先求得两个链表的长度,然后得到长度差diff,再先遍历长链表diff步后,再同时遍历两个链表并比较对象指针. /* public class ListNode { int val; List ...
- websocket简单理解
实现及原理 Websocket是一种在单个TCP连接上进行全双工通讯的协议. WebSocket 首先发起一个 HTTP 请求,在请求头加上 `Upgrade` 字段,该字段用于改变 HTTP 协议版 ...
- oracle体系结构理解
体系结构相关内容每次看遍书,过段时间就忘了..无奈用自己理解的方式记录之. 1.commit与写盘与否没有关系,也就是说修改数据(insert update delete)后并提交数据,这条被修改的数 ...