72. 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
* Dynamic Programming
* Definitaion
* m[i][j] is minimal distance from word1[0..i] to word2[0..j]
* So,
* 1) if word1[i] == word2[j], then m[i][j] == m[i-1][j-1].
* 2) if word1[i] != word2[j], then we need to find which one below is minimal:
* min( m[i-1][j-1], m[i-1][j], m[i][j-1] ) and +1 - current char need be changed.
* Let's take a look m[1][2] : "a" => "ab"
* +---+ +---+
* ''=> a | 1 | | 2 | '' => ab
* +---+ +---+
* +---+ +---+
* a => a | 0 | | 1 | a => ab
* +---+ +---+
*
* To know the minimal distance `a => ab`, we can get it from one of the following cases:
* 1) delete the last char in word1, minDistance( '' => ab ) + 1
* 2) delete the last char in word2, minDistance( a => a ) + 1
* 3) change the last char, minDistance( '' => a ) + 1
* For Example:
* word1="abb", word2="abccb"
* 1) Initialize the DP matrix as below:
* "" a b c c b
* "" 0 1 2 3 4 5
* a 1
* b 2
* b 3
* 2) Dynamic Programming
* "" a b c c b
* "" 0 1 2 3 4 5
* a 1 0 1 2 3 4
* b 2 1 0 1 2 3
* b 3 2 1 1 1 2
int min(int x, int y, int z) {
return std::min(x, std::min(y,z));
} int minDistance(string word1, string word2) {
int n1 = word1.size();
int n2 = word2.size();
if (n1==) return n2;
if (n2==) return n1;
vector< vector<int> > m(n1+, vector<int>(n2+));
for(int i=; i<m.size(); i++){
m[i][] = i;
}
for (int i=; i<m[].size(); i++) {
m[][i]=i;
} //Dynamic Programming
int row, col;
for (row=; row<m.size(); row++) {
for(col=; col<m[row].size(); col++){
if (word1[row-] == word2[col-] ){
m[row][col] = m[row-][col-];
}else{
int minValue = min(m[row-][col-], m[row-][col], m[row][col-]);
m[row][col] = minValue + ;
}
}
} return m[row-][col-];
}
72. Edit Distance *HARD*的更多相关文章
- 【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] 72. Edit Distance 编辑距离
Given two words word1 and word2, find the minimum number of operations required to convert word1 to ...
- 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 (编辑距离) 解题思路和方法
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(困难,确实挺难的,但很经典,双序列DP问题)
Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2 ...
- 【一天一道LeetCode】#72. Edit Distance
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given t ...
随机推荐
- 01: vue.js安装
1.1 vue.js安装与基本使用 官网:https://cn.vuejs.org/ 1.使用之前,我们先来掌握3个东西是用来干什么的 1. npm: Nodejs下的包管理器. 2. webpack ...
- 20145321 《网络对抗》 Web基础
20145321 <网络对抗> Web基础 基础问题回答 (1)什么是表单 表单在网页中主要负责数据采集功能,一个表单有三个基本组成部分:表单标签——这里面包含了处理表单数据所用CGI程序 ...
- git_如何查看两个版本之间那些文件被修改
需求 由于工程比较庞大,接近500M,每部署一次如果都全量部署,不仅仅磁盘空间耗费较大,最主要是要等很长时间, 之前就一直有这个问题,但是也就没有弄,上周领导发话了,这个问题必须要解决了 前记 增加带 ...
- Go第三篇之大话容器
Go语言数组 数组(Array)是一段固定长度的连续内存区域 在 Go 语言中,数组从声明时就确定,使用时可以修改数组成员,但是数组大小不可变化 Go 语言数组的声明 数组的写法如下: var 数组变 ...
- POJ 2785 4 Values whose Sum is 0 (二分)题解
思路: 如果用朴素的方法算O(n^4)超时,这里用折半二分.把数组分成两块,分别计算前后两个的和,然后枚举第一个再二分查找第二个中是否有满足和为0的数. 注意和有重复 #include<iost ...
- rg.apache.ibatis.binding.BindingException: Mapper method 'com.dao.Cameao.getOnlineDayRation attempted to return null from a method with a primitive return type (float)
本文为博主原创,未经允许不得转载: 异常展示如下: org.apache.ibatis.binding.BindingException: Mapper method 'com.dao.Cameao. ...
- Java中Arrays 与 Collections 的简单操作
import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; import java.util.C ...
- package 'orocos-bfl' not found CMake Error at /usr/share/cmake-2.8/Modules/FindPkgConfig.cmake:283 (message):
#没有数字 sudo apt-get install ros-indigo-bfl
- Jmeter自动化测试 数据驱动测试,将数据存入csv文件中来调用,或将数据存在DB中进行调用
1. 将测试的用例名称,测试请求方式,测试链接,预置数据,断言等都放到excel中,然后转成csv格式,在用Jmeter带的csv数据配置文件导入 运行之前将线程组中配置,线程数设置为1,循环的次数设 ...
- Jmeter 抓app包 抓到一半不好用了
错误描述: java.net.ConnectException: Connection refused (Connection refused) at java.net.PlainSocketImpl ...