leetcode第72题:编辑距离
给定两个单词 word1 和 word2,计算出将 word1 转换成 word2 所使用的最少操作数 。
你可以对一个单词进行如下三种操作:
- 插入一个字符
- 删除一个字符
- 替换一个字符
示例 1:
输入: word1 = "horse", word2 = "ros"
输出: 3
解释:
horse -> rorse (将 'h' 替换为 'r')
rorse -> rose (删除 'r')
rose -> ros (删除 'e')
示例 2:
输入: word1 = "intention", word2 = "execution"
输出: 5
解释:
intention -> inention (删除 't')
inention -> enention (将 'i' 替换为 'e')
enention -> exention (将 'n' 替换为 'x')
exention -> exection (将 'n' 替换为 'c')
exection -> execution (插入 'u'
网易面试时遇到了这题,当时没做出来,想不出来状态转移方程。
解题思路如下:
首先定义状态矩阵,dp[m][n],其中m为word1的长度+1,n为word2的长度+1,为什么+1?因为要考虑如果word1或word2为空的情况,后面可以看到。
定义dp[i][j]为word1中前i个字符组成的串,与word2中前j个字符组成的串的编辑距离。
插入操作:在word1的前i个字符后插入一个字符,使得插入的字符等于新加入的word2[j]。这里要考虑清楚,插入操作对于原word1字符来说,i是没有前进的,而对于word2来说是前进了一位然后两个字符串才相等的。所以此时是dp[i][j]=dp[i][j-1]+1。
删除操作:在word1的第i−1个字符后删除一个字符,使得删除后的字符串word[:i-1]与word2[:j]相同。这里要考虑清楚,删除操作对于原word2字符来说,j−1是没有前进的,而对于word1来说是删除了一位然后两个字符串才相等的。所以此时是dp[i][j]=dp[i-1][j]+(0 or 1)。
代码如下:
class Solution: def minDistance(self, word1, word2):
m=len(word1)+1; n=len(word2)+1
dp = [[0 for i in range(n)] for j in range(m)] for i in range(n):
dp[0][i]=i
for i in range(m):
dp[i][0]=i
for i in range(1,m):
for j in range(1,n):
if word1[i-1] == word2[j-1]:
dp[i][j] = dp[i-1][j-1]
else:
dp[i][j] = min(dp[i][j-1], dp[i-1][j], dp[i-1][j-1]) + 1 return dp[m-1][n-1] word1 = "intention"
word2 = "execution"
test = Solution()
print(test.minDistance(word1, word2))
答案来源:https://blog.csdn.net/iyuanshuo/article/details/80112211
def minDistance(self, S1, S2):
m = len(S1) + 1;
n = len(S2) + 1
dp = [[0 for i in range(n)] for j in range(m)]
for i in range(n):
dp[0][i] = i
for i in range(m):
dp[i][0] = i
for i in range(1, m):
for j in range(1, n):
if S1[i - 1] == S2[j - 1]:
dp[i][j] = dp[i - 1][j - 1]
else:
dp[i][j] = min(dp[i][j - 1], dp[i - 1][j], dp[i - 1][j - 1]) + 1
return dp[m - 1][n - 1]
leetcode第72题:编辑距离的更多相关文章
- Leetcode OJ 刷题
Valid Palindrome吐槽一下Leetcode上各种不定义标准的输入输出(只是面试时起码能够问一下输入输出格式...),此篇文章不是详细的题解,是自己刷LeetCode的一个笔记吧,尽管没有 ...
- [LeetCode] 系统刷题5_Dynamic Programming
Dynamic Programming 实际上是[LeetCode] 系统刷题4_Binary Tree & Divide and Conquer的基础上,加上记忆化的过程.就是说,如果这个题 ...
- leetcode 第188题,我的解法,Best Time to Buy and Sell Stock IV
<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255) ...
- leetcode第37题--Count and Say
题目:(据说是facebook的面试题哦) The count-and-say sequence is the sequence of integers beginning as follows:1, ...
- LeetCode第[18]题(Java):4Sum 标签:Array
题目难度:Medium 题目: Given an array S of n integers, are there elements a, b, c, and d in S such that a + ...
- LeetCode第[1]题(Java):Two Sum 标签:Array
题目: Given an array of integers, return indices of the two numbers such that they add up to a specifi ...
- LeetCode的刷题利器(伪装到老板都无法diss你没有工作)
在工程效率大行其道的今天,如果不会写点代码以后也不容易在测试圈混下去.今天给大家推荐一个LeetCode的刷题利器,可以伪装到连你老板在这里走过去都无法确认你是在干活呢,还是在干活呢. LeetCod ...
- LeetCode每天一题之两数之和
这个LeetCode刷题系列的博客权当是为自己记一下笔记吧.博客系列会从LeetCode的第一题开始刷,同时会从零开始学习[因为我就是零/(ㄒoㄒ)/~~].同时,如果有写错的地方,希望大佬们在评论区 ...
- leetcode第三题
leetcode第三题: 题目: 给定一个字符串,找出不含有重复字符的最长子串的长度. 源码(使用java语言): class Solution { public int lengthOfLonges ...
随机推荐
- appium+python+unittest+HTMLRunner编写UI自动化测试集
简介 获取AppPackage和AppActivity 定位UI控件的工具 脚本结构 PageObject分层管理 HTMLTestRunner生成测试报告 启动appium server服务 以py ...
- 深入解析 composer 的自动加载原理 (转)
深入解析 composer 的自动加载原理 转自:https://segmentfault.com/a/1190000014948542 前言 PHP 自5.3的版本之后,已经重焕新生,命名空间.性状 ...
- cmd下可以启动java,输入javac提示 不“存在”
方法:手动把JDK安装目录的bin目录配置到PATH环境变量里
- summary ranges leetcode java
问题描述: Given a sorted integer array without duplicates, return the summary of its ranges. For example ...
- 惊世骇俗的sql语句之连表查询
select `product_skus`.id as skuId, `wname` as sku名称, if(`sku_attributes`.`status`=1,'上架','下架') as 状态 ...
- POJ No.2386 Lake Counting
题目链接:http://poj.org/problem?id=2386 分析:八联通的则为水洼,我们则需遍历一个单位附近的八个单位并将它们都改成'.',但附近单位可能仍连接着有'W'的区域,这种情况下 ...
- Python图片缩放
from PIL import Image def size(jpg,now_size): im = Image.open(jpg) width, height = im.size if width& ...
- mac终端下连接阿里云服务器
通过ssh连接 ssh 用户名@地址 ssh root@xx.xxx.xxx.xx https://www.jianshu.com/p/f034817a7837 最后还是通过 FileZilla 连 ...
- 字符串加密解密(Base64)
var Base64 = { // private property _keyStr: "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwx ...
- leetcode-algorithms-17 Letter Combinations of a Phone Number
leetcode-algorithms-17 Letter Combinations of a Phone Number Given a string containing digits from 2 ...