leetcode72. Edit Distance(编辑距离)
- 以下为个人翻译方便理解
- 编辑距离问题是一个经典的动态规划问题。首先定义dp[i][j表示word1[0..i-1]到word2[0..j-1]的最小操作数(即编辑距离)。
- 状态转换方程有两种情况:边界情况和一般情况,以上表示中 i和j均从1开始(注释:即至少一个字符的字符串向一个字符的字符串转换,0字符到0字符转换编辑距离自然为0)
- 1.边界情况:将一个字符串转化为空串,很容易看出把word[0...i-1]转化成空串“”至少需要i次操作(注释:i次删除),则其编辑距离为i,即:dp[i][0] = i;dp[0][j] = j.
2.一般情况:转化非空字符串word1[0..i - 1] 为另一非空字符串 word2[0..j - 1],此处问题转化为几个个子问题:假定已知word1[0..i - 2] 到 word2[0..j - 2]的编辑距离, 即dp[i - 1][j - 1],只需考虑word[i - 1] 和 word2[j - 1]
- 如果 word[i - 1] == word2[j - 1],无需操作即可满足word1[0..i - 1] 与 word2[0..j - 1]相同,则编辑距离dp[i][j] = dp[i - 1][j - 1]
- 如果 word[i - 1] != word2[j - 1],分为三种子情况:
- 用 word2[j - 1]替换word1[i - 1],则有 (dp[i][j] = dp[i - 1][j - 1] + 1 (一次操作用于替换));
- 删除 word1[i - 1] 使得 word1[0..i - 2] = word2[0..j - 1],则有(dp[i][j] = dp[i - 1][j] + 1 (一次操作用于删除));
- 在word1[0..i - 1] 中插入 word2[j - 1] 使得 word1[0..i - 1] + word2[j - 1] = word2[0..j - 1] ,则有(dp[i][j] = dp[i][j - 1] + 1 (一次操作用于插入)).
为了保证理解插入和删除带来的细微差别,对于删除,其实是将word1[0..i - 2] 转化成 word2[0..j - 1], 编辑距离是 dp[i - 1][j],之后直接删除word1[i - 1],一次操作,插入也是类似
(注释:就是由word1[0..i - 2] 编辑转化成 word2[0..j - 1],删除word1[i - 1]两个操作共同完成实现将word1[0..i - 1] 转化成 word2[0..j - 1])- 合并规如下:
- dp[i][0] = i;
- dp[0][j] = j;
- dp[i][j] = dp[i - 1][j - 1], if word1[i - 1] = word2[j - 1];
- dp[i][j] = min(dp[i - 1][j - 1] + 1, dp[i - 1][j] + 1, dp[i][j - 1] + 1)
- 转化为代码如下
'''
class Solution {
public:
int minDistance(string word1, string word2) {
int m = word1.length(), n = word2.length();
vector - 你可能会注意到每次更新dp[i][j],我们只需要dp[i - 1][j - 1], dp[i][j - 1], dp[i - 1][j]就行
- 事实上我们不必维护整个m*n矩阵。相反维护一栏即可,代码空间复杂度降为O(m)或者O(n)取决于你维护的的是矩阵的一行还是一列
优化后代码如下:
'''
class Solution {
public:
int minDistance(string word1, string word2) {
int m = word1.length(), n = word2.length();
vector
leetcode72. Edit Distance(编辑距离)的更多相关文章
- Edit Distance编辑距离(NM tag)- sam/bam格式解读进阶
sam格式很精炼,几乎包含了比对的所有信息,我们平常用到的信息很少,但特殊情况下,我们会用到一些较为生僻的信息,关于这些信息sam官方文档的介绍比较精简,直接看估计很难看懂. 今天要介绍的是如何通过b ...
- [LeetCode] Edit Distance 编辑距离
Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2 ...
- 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 编辑距离
Given two words word1 and word2, find the minimum number of operations required to convert word1 to ...
- [leetcode72]Edit Distance(dp)
题目链接:https://leetcode.com/problems/edit-distance/ 题意:求字符串的最短编辑距离,就是有三个操作,插入一个字符.删除一个字符.修改一个字符,最终让两个字 ...
- leetcode72. Edit Distance
Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2 ...
- 【LeetCode每天一题】Edit Distance(编辑距离)
Given two words word1 and word2, find the minimum number of operations required to convert word1 to ...
- 【LeetCode】72. Edit Distance 编辑距离(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 记忆化搜索 动态规划 日期 题目地址:http ...
- edit distance(编辑距离,两个字符串之间相似性的问题)
Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2 ...
随机推荐
- Static List
Static ListStatic List is the smart implementation of list data structure for those languages that h ...
- php的一些问题
1.关于php <? php echo "hello world"; include "./index.html"; require "./in ...
- jsp连接SQL Server数据库的方式
方式1:JDBC连接方式 Connection conn = null; Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDrive ...
- var关键字获取数据类型
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threa ...
- 面向amd64的XXX与与项目的目标平台“x86”不兼容
打开IIS服务器,选择应用程序池,设置中,有一个打开32位程序,选择FALSE,如果开启,在64位下就会出错.一般关闭
- jsr133
1:介绍 java虚拟机支持多线程运行.线程代表的就是Thread class.对用户来说创建线程的唯一办法就是创建一个Thread对象:每一个线程都和一个Thread对象关联.Thread对象调用s ...
- JSONP和CORS两种跨域方式的简单介绍和解决方案实例
随着软件开发分工趋于精细,前后端开发分离成为趋势,前端同事负责前端页面的展示及页面逻辑处理,服务端同事负责业务逻辑处理同时通过API为前端提供数据也为前端提供数据的持久化能力,考虑到前后端同事开发工具 ...
- Nginx快速入门菜鸟笔记
Nginx快速入门-菜鸟笔记 1.编译安装nginx 编译安装nginx 必须先安装pcre库. (1)uname -a 确定环境 Linux localhost.localdomain 2.6. ...
- [转载]jQuery1.9完全删除live事件
其实关于live被删除的事件早就在1.7.2版本以后就有通知过了 不过在官方一直提倡使用.on代替.live的过程中 .live事件一直沿用 今天更新了 看下jQuery1.9的相关信息 发现api中 ...
- Intellij IDEA +MAVEN+Jetty实现Mybatis的HelloWorld
1 maven配置:pom.xml <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="ht ...