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 ...
随机推荐
- C++入门经典-例2.14-使用移位运算
1:代码如下: // 2.14.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include <iostream> usin ...
- ES6 知识拓展
1.冻结对象 Object.freeze(obj) 方法可以冻结一个对象,冻结指的是不能向这个对象添加新的属性,不能修改其已有属性的值,不能删除已有属性,以及不能修改该对象已有属性的可枚举性.可配置性 ...
- 一、linux下nginx1.7.8+php5.6.3的环境配置
部分参考:http://www.cnblogs.com/simpman/p/4151662.html http://blog.zyan.cc/nginx_php_v6 1.利用linux系统自带的yu ...
- html 行内元素和块级元素
行内元素一般是内容的容器,而块级元素一般是其他容器的容器.一般情况下,行内元素只能包含内容或者其它行内元素,宽度和长度依据内容而定,不可以设置,可以和其它元素和平共处于一行:而块级元素可以包含行内元素 ...
- linux挂载本地镜像
//创建挂载目录 mkdir /media/cdrom //挂载镜像 mount -t iso9660 /dev/cdrom /media/cdrom 提示:mount:block device /d ...
- navicat常用快捷键与SQL基本使用
一.Navicat常用快捷键 1,Ctrl+q就会弹出一个sql输入窗口 2,Ctrl+r就执行sql了 3,按f6会弹出一个命令窗口 4,Ctrl+/ 注释 5,Ctrl +Shift+/ 解除注释 ...
- [C#菜鸟]C# Hook (二) 常用钩子的类型
; //监视和记录输入事件.安装一个挂钩处理过程,对寄送至系统消息队列的输入消息进行纪录 ; //回放用WH_JOURNALRECORD记录事件 ; //键盘钩子,键盘触发消息.WM_KEYUP或WM ...
- java 设计模式 单例模式之饿汉模式/懒汉模式 singleton pattern
https://v.qq.com/x/page/e0364ung5zp.html 讲的不错, 关于 饿汉式单例模式 code Student 类: package com.test;//单例模式之 ...
- inner join, left join, right join, full outer join的区别
总的来说,四种join的区别可以描述为: left join 会从左表(shop)那里返回所有的记录,即使在右表(sale_detail)中没有匹配的行. right outer join 右连接,返 ...
- android中builder模式的使用
变种的Builder模式的自动化生产实现: AS安装插件 Innerbuilde 新建User类 public class User { private final String name; //必 ...