【LeetCode每天一题】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
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') 思路
这道题是一道典型的使用动态规划来解决的题目。两个单词我们申请一个(m+1)*(n+1)的矩阵,首先对矩阵的第一行和第一列进行初始化,然后从第二行第二个位置开始进行遍历,每次得到最小的编辑数。 这里如果当前两个字母相等的话,直接使其等于上一个字母的编辑数,也即dp[i][j] = dp[i-1][j-1]。但是当两个字母不相等的时候,我们可以从左边上边和右上角选出最小的编辑数在加一,得到当前位置的编辑数,也即dp[i][j] = min(dp[i-1][j-1], min(dp[i-1][j], dp[i][j-1]))+1。这样直到循环遍历到矩阵的末尾。最后一个数字也即是最小编辑距离。时间复杂度为O(m*n),空间复杂度为O(m*n)。
一般对于动态规划来题目来说,我们除了设置一个(m+1)*(n+1)的矩阵外,还可以使用(n+1)大小的矩阵。这里动态方程还是一样的,只不过这里我们需要处理的细节更多一些。时间复杂度和上面的一样,空间复杂度为O(n+1)。
图示步骤
解决代码
第一种空间复杂度为O(m*n)的解法
class Solution(object):
def minDistance(self, word1, word2):
"""
:type word1: str
:type word2: str
:rtype: int
"""
if not word1 or not word2: # 一个为空直接返回另一个不为空的长度。
return len(word1) if not word2 else len(word2) m, n= len(word1), len(word2)
dp = []
for i in range(m+1): # 构造辅助矩阵
dp.append([0]*(n+1)) for i in range(1, m+1): # 初始化第一列
dp[i][0] = i for j in range(1, n+1): # 初始化第一行
dp[0][j] = j for i in range(1, m+1): # 逐个求解
for j in range(1, n+1):
if word1[i-1] == word2[j-1]: # 当前字母相等时,
dp[i][j] = dp[i-1][j-1]
else: # 不相等时
dp[i][j] = min(dp[i-1][j-1], min(dp[i-1][j], dp[i][j-1]))+1
return dp[m][n]
空间复杂度为O(n)的解法
class Solution(object):
def minDistance(self, word1, word2):
"""
:type word1: str
:type word2: str
:rtype: int
"""
if not word1 or not word2:
return len(word1) if not word2 else len(word2)
m, n= len(word1), len(word2)
dp = [0]*(n+1) # 申请辅助数据
for i in range(1, n+1): # 初始化第一行
dp[i] = i for i in range(1,m+1): # 循环遍历
pre = dp[0] # 记录下dp[0]的值,也即为上面矩阵中dp[i-1][j-1]的值。
dp[0]= i # 给dp[0]赋值为当前单词编辑列的距离,也就是上面的初始化第一列
for j in range(1, n+1):
tem = dp[j] # 相当于记录下dp[i][j-1]的值,
if word1[i-1] == word2[j-1]: # 单词相等的时候
dp[j] = pre
else:
dp[j] = min(pre, min(dp[j-1], dp[j]))+1
pre = tem # 更新值 return dp[-1]
【LeetCode每天一题】Edit Distance(编辑距离)的更多相关文章
- [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 编辑距离
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 ...
- Edit Distance编辑距离(NM tag)- sam/bam格式解读进阶
sam格式很精炼,几乎包含了比对的所有信息,我们平常用到的信息很少,但特殊情况下,我们会用到一些较为生僻的信息,关于这些信息sam官方文档的介绍比较精简,直接看估计很难看懂. 今天要介绍的是如何通过b ...
- 【LeetCode】161. One Edit Distance
Difficulty: Medium More:[目录]LeetCode Java实现 Description Given two strings S and T, determine if the ...
- LeetCode解题报告—— N-Queens && Edit Distance
1. N-Queens The n-queens puzzle is the problem of placing n queens on an n×n chessboard such that no ...
- LeetCode(72) Edit Distance
题目 Given two words word1 and word2, find the minimum number of steps required to convert word1 to wo ...
- 【LeetCode】72. Edit Distance 编辑距离(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 记忆化搜索 动态规划 日期 题目地址:http ...
- Java解决LeetCode72题 Edit Distance
题目描述 地址 : https://leetcode.com/problems/edit-distance/description/ 思路 使用dp[i][j]用来表示word1的0~i-1.word ...
随机推荐
- windows 查看端口占用,杀进程
查看 443端口占用 netstat -ano | findstr "443" ,得到如下信息: TCP [::]:443 [::]:0 LISTENING 2320 发现是被23 ...
- Angular项目中共享模块的实现
创建share Modele:ng g m share import进来所有需要共享的模块都export出去, 一.共享CommonModule 暂时只有CommonModule,以后会有一些需要共享 ...
- 动态dp
题解: 首先这类题目本身是一个dp/树形dp 然后带上了修改(ddp) 为了权衡查询和修改的时间,我们采用树剖来维护 假设我们能够对每个点维护除了重儿子之外的信息 那么我们的修改只需要修改log个节点 ...
- Java-正则使用
Java-正则使用 注意 在Java中由于string的设计,导致斜杠是特殊的字符,所以如若想要在正则中使用斜杠,则需要两个斜杠来表示 eg: \d 需要写成: \\d ,两外 \\\\ 表示匹配单个 ...
- DAPP 开发直通车-如何基于NEL 轻钱包来开发DAPP
之前做了 DAPP 开发直通车,通讲了一下开发一个DAPP的过程. 但是涉及多工种,多步骤.入手还是非常困难的. 经过不懈的努力,做了很多铺垫工作之后,我终于可以告诉你: 开发DAPP f ...
- 2017-11-4—LTspice
LTspice这个软件基本上上手就能用,没什么好说的. ADI的一些器件下载的spice文件可能是.cir的需要使用cadence的pspice软件打开后另存为.dir文件使用.(比如ad4096) ...
- 2017-2018 ACM-ICPC, Central Europe Regional Contest (CERC 17)
A. Assignment Algorithm 按题意模拟即可. #include<stdio.h> #include<iostream> #include<string ...
- 搭建自己的Webpack项目
五,搭建自己的Webpack项目 https://www.jianshu.com/p/42e11515c10f
- CSS3常用
1.user-select新增特性,主流浏览器都支持 -webkit-user-select: none; /* Chrome all / Safari all /opera15+*/ -moz- ...
- Mybatis_1(认识)一个简单的HelloWorld
1. 介绍: MyBatis是支持普通SQL查询,存储过程和高级映射的优秀持久层框架. MyBatis消除了几乎所有的JDBC代码和参数的手工设置以及对结果集的检索. MyBatis可以使用简单的XM ...