[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 ...
随机推荐
- Spring Cloud(Dalston.SR5)--Config 集群配置中心
Spring Cloud Config 是一个全新的项目,用来为分布式系统中的基础设施和微服务应用提供集中化的外部配置支持,他分为服务端和客户端两个部分.服务端也称为分布式配置中心,是一个独立的微服务 ...
- 创建一个HTTP接口测试
jmeter Apache JMeter是Apache组织开发的基于Java的压力测试工具. Apache jmeter 可以用于对静态的和动态的资源(文件,Servlet,Perl脚本,java 对 ...
- Scrapy实战篇(五)之爬取历史天气数据
本篇文章我们以抓取历史天气数据为例,简单说明数据抓取的两种方式: 1.一般简单或者较小量的数据需求,我们以requests(selenum)+beautiful的方式抓取数据 2.当我们需要的数据量较 ...
- pt-table-checksum校验与pt-table-sync修复数据
1:下载工具包 登录网站下载相应的工具包 https://www.percona.com/downloads/percona-toolkit/LATEST/ 2:安装 (1)yum安装: sudo y ...
- SAS 评分卡开发模型变量统计及输出
以下代码实现功能: 1.获取10个模型分别使用哪些变量 2.变量所模型使用的次数 3.把上表格输出到EXCEL中 %INCLUDE '00@HEADER.SAS'; %let dir=..\04@Mo ...
- Android Studio 制作一个循环播报的效果
这个就是用到了一个TextView 控件,直接上代码. <TextView android:id="@+id/tv_7" android:layout_width=" ...
- 用Jedis调用Lua脚本来完成redis的数据操作
1.先完成一个简单的set/get操作 package com.example.HnadleTaskQueue; import redis.clients.jedis.Jedis; import ja ...
- OOM三种情况
第一种OutOfMemoryError: PermGen space发生这种问题的原意是程序中使用了大量的jar或class,使java虚拟机装载类的空间不够,与Permanent Generatio ...
- Web缓存机制
Web 缓存的出现主要是为了弥补cookie带来的一些局限,当数据严格控制在客户端时,可以不用通过HTTP来持续得将数据发给服务器. 主要使用比较多的就是sessionStorage和localSto ...
- 析构方法(__del__)
析构方法,当对象在内存中被释放时(也就是实例执行完了,实例的内存就会自动释放,这时候就会触发),自动触发执行. 当程序结束时,python只会回收自己的内存空间,即用户态内存,而操作系统的资源则没有被 ...