72. Edit Distance (JAVA)
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
Example 1:
Input: word1 = "horse", word2 = "ros"
Output: 3
Explanation:
horse -> rorse (replace 'h' with 'r')
rorse -> rose (remove 'r')
rose -> ros (remove 'e')
Example 2:
Input: word1 = "intention", word2 = "execution"
Output: 5
Explanation:
intention -> inention (remove 't')
inention -> enention (replace 'i' with 'e')
enention -> exention (replace 'n' with 'x')
exention -> exection (replace 'n' with 'c')
exection -> execution (insert 'u')
使用递归会造成Time limit exceeded
class Solution {
public int minDistance(String word1, String word2) {
StringBuffer strBuf1 = new StringBuffer(word1);
StringBuffer strBuf2 = new StringBuffer(word2); return dfs(strBuf1,strBuf2,0,0,0);
} public int insert(StringBuffer strBuf1, StringBuffer strBuf2, int i1, int i2, int depth){
strBuf1.insert(i1, strBuf2.charAt(i2));
int ret = dfs(strBuf1,strBuf2,i1+1, i2+1,depth+1);
strBuf1.deleteCharAt(i1); //recover
return ret;
} public int delete(StringBuffer strBuf1, StringBuffer strBuf2, int i1, int i2, int depth){
Character ch = strBuf1.charAt(i1);
strBuf1.deleteCharAt(i1);
int ret = dfs(strBuf1,strBuf2,i1, i2,depth+1);
strBuf1.insert(i1,ch); //recover;
return ret;
} public int replace(StringBuffer strBuf1, StringBuffer strBuf2, int i1, int i2, int depth){
Character ch = strBuf1.charAt(i1);
strBuf1.setCharAt(i1, strBuf2.charAt(i2));
int ret = dfs(strBuf1,strBuf2,i1+1, i2+1,depth+1);
strBuf1.setCharAt(i1, ch);
return ret;
} private int dfs(StringBuffer strBuf1, StringBuffer strBuf2, int i1, int i2, int depth){
while(i1 < strBuf1.length() && i2 < strBuf2.length() && strBuf1.charAt(i1) == strBuf2.charAt(i2)){
i1++;
i2++;
} if(i1 == strBuf1.length() && i2 == strBuf2.length()) return depth;
if(i1 == strBuf1.length()) return depth+strBuf2.length()-i2;
if(i2 == strBuf2.length()) return depth+strBuf1.length()-i1; int ret = insert(strBuf1,strBuf2,i1,i2,depth);
ret = Math.min(ret,delete(strBuf1,strBuf2,i1,i2,depth));
ret = Math.min(ret,replace(strBuf1,strBuf2,i1,i2,depth)); return ret; } }
使用动态规划dp[i][j]表示从word1[i+1]位置到word2[j+1]位置 需要改变次数。
class Solution {
public int minDistance(String word1, String word2) {
int[][] dp = new int[word1.length()+1][word2.length()+1];
for(int i = 0; i <= word1.length(); i++){
dp[i][0] = i;
}
for(int j = 0; j <= word2.length(); j++){
dp[0][j] = j;
}
for(int i = 1; i <= word1.length(); i++){
for(int j = 1; j <= word2.length(); j++){
if(word1.charAt(i-1) == word2.charAt(j-1)){
dp[i][j] = dp[i-1][j-1];
}
else{
dp[i][j] = 1+Math.min(dp[i-1][j-1],Math.min(dp[i-1][j],dp[i][j-1])); //insert & replace: dp[i-1][j-1] +1; delete: dp[i-1][j],dp[i][j-1]
}
}
} return dp[word1.length()][word2.length()];
} }
72. Edit Distance (JAVA)的更多相关文章
- 【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! 二.我的解答 这个题目一 ...
- 72. Edit Distance
题目: Given two words word1 and word2, find the minimum number of steps required to convert word1 to w ...
- [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 (编辑距离) 解题思路和方法
Edit Distance Given two words word1 and word2, find the minimum number of steps required to convert ...
- [LeetCode] 72. Edit Distance(最短编辑距离)
传送门 Description Given two words word1 and word2, find the minimum number of steps required to conver ...
- LeetCode - 72. Edit Distance
最小编辑距离,动态规划经典题. Given two words word1 and word2, find the minimum number of steps required to conver ...
- 72. Edit Distance *HARD*
Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2 ...
- 72. Edit Distance(困难,确实挺难的,但很经典,双序列DP问题)
Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2 ...
随机推荐
- 使用layui异步请求上传图片在tp5.1环境下出现“请对上传接口返回json”的错误的解决方法
正常情况下返回json数据使用return json(); 但是使用layui会报错,然后想到了使用json_encode()包装一下用一个变量接收后,再使用return();返回接收json格式的变 ...
- AJAX请求和普通HTTP请求区别
两者本质区别: AJAX通xmlHttpRequest象请求服务器服务器接受请求返数据实现刷新交互 普通http请求通httpRequest象请求服务器接受请求返数据需要页面刷新 AJAX请求 普通请 ...
- i 是一个修饰符 (搜索不区分大小写)
什么是正则表达式? 正则表达式是由一个字符序列形成的搜索模式. 当你在文本中搜索数据时,你可以用搜索模式来描述你要查询的内容. 正则表达式可以是一个简单的字符,或一个更复杂的模式. 正则表达式可用于所 ...
- 【Spark机器学习速成宝典】模型篇04朴素贝叶斯【Naive Bayes】(Python版)
目录 朴素贝叶斯原理 朴素贝叶斯代码(Spark Python) 朴素贝叶斯原理 详见博文:http://www.cnblogs.com/itmorn/p/7905975.html 返回目录 朴素贝叶 ...
- RF-创建一个自定义关键字库
仓库自定义库 这里以Selenium2Library库进行举例说明: 编写一个自定义仓库类(与库文件夹名一致),继承关键字类,指定范围和版本即可. 需要声明__init__. import os fr ...
- T83312 【音乐会】达拉崩吧·上
T83312 [音乐会]达拉崩吧·上 题解 线段树板子题 把原来的 + 变成 ^ 但是注意一下懒标记,这里讲一下小技巧 代码 #include<bits/stdc++.h> using n ...
- 清明 DAY 3
ans=1000*4 分别固定千位,百位,十位,个位为1,其余位置随便排 对于每一个质因数的n次方,共有n+1中选择方法,即这个质因数的0~n次方 故共有 4*3*5=60 种方法 (1)取两册 ...
- DatePicker 日期选择器
用于选择或输入日期 选择日 以「日」为基本单位,基础的日期选择控件 基本单位由type属性指定.快捷选项需配置picker-options对象中的shortcuts,禁用日期通过 disabledDa ...
- 使用 tcpdump 抓包分析 TCP 三次握手、四次挥手与 TCP 状态转移
目录 文章目录 目录 前文列表 TCP 协议 图示三次握手与四次挥手 抓包结果 抓包分析 TCP 三次握手 数据传输 四次挥手 TCP 端口状态转移 状态转移 前文列表 <常用 tcpdump ...
- EINT DINT ERTM DRTM EALLOW EDIS ESTOP0的理解
本文参考以下资料整理 https://wenku.baidu.com/view/6b0d6906cf84b9d528ea7a66.html http://pangqicheng123.blog.163 ...