原题地址:https://oj.leetcode.com/problems/edit-distance/

题意:

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

解题思路:这道题是很有名的编辑距离问题。用动态规划来解决。状态转移方程是这样的:dp[i][j]表示word1[0...i-1]到word2[0...j-1]的编辑距离。而dp[i][0]显然等于i,因为只需要做i次删除操作就可以了。同理dp[0][i]也是如此,等于i,因为只需做i次插入操作就可以了。dp[i-1][j]变到dp[i][j]需要加1,因为word1[0...i-2]到word2[0...j-1]的距离是dp[i-1][j],而word1[0...i-1]到word1[0...i-2]需要执行一次删除,所以dp[i][j]=dp[i-1][j]+1;同理dp[i][j]=dp[i][j-1]+1,因为还需要加一次word2的插入操作。如果word[i-1]==word[j-1],则dp[i][j]=dp[i-1][j-1],如果word[i-1]!=word[j-1],那么需要执行一次替换replace操作,所以dp[i][j]=dp[i-1][j-1]+1,以上就是状态转移方程的推导。

代码:

class Solution:
# @return an integer
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):
dp[i][j]=min(dp[i-1][j]+1, dp[i][j-1]+1, dp[i-1][j-1]+(0 if word1[i-1]==word2[j-1] else 1))
return dp[m-1][n-1]

[leetcode]Edit Distance @ Python的更多相关文章

  1. [LeetCode] Edit Distance 编辑距离

    Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2 ...

  2. Leetcode:Edit Distance 解题报告

    Edit Distance Given two words word1 and word2, find the minimum number of steps required to convert  ...

  3. [LeetCode] Edit Distance 字符串变换为另一字符串动态规划

    Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2 ...

  4. Leetcode Edit Distance

    Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2 ...

  5. [LeetCode] Edit Distance(很好的DP)

    Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2 ...

  6. LeetCode: Edit Distance && 子序列题集

    Title: Given two words word1 and word2, find the minimum number of steps required to convert word1 t ...

  7. LeetCode——Edit Distance

    Question Given two words word1 and word2, find the minimum number of steps required to convert word1 ...

  8. [LeetCode] One Edit Distance 一个编辑距离

    Given two strings S and T, determine if they are both one edit distance apart. 这道题是之前那道Edit Distance ...

  9. Java for LeetCode 072 Edit Distance【HARD】

    Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2 ...

随机推荐

  1. 公钥,密钥,HTTPS详解

    公钥与私钥,HTTPS详解 1.公钥与私钥原理1)鲍勃有两把钥匙,一把是公钥,另一把是私钥2)鲍勃把公钥送给他的朋友们----帕蒂.道格.苏珊----每人一把.3)苏珊要给鲍勃写一封保密的信.她写完后 ...

  2. [代码审计]yxcms从伪xss到getshell

    0x00 前言 这篇文章首发于圈子,这里作为记录一下. 整个利用链构造下来是比较有趣的,但实际渗透中遇到的几率比较少. 此次审的是yxcms 1.4.6版本,应该是最后一个版本了吧? 0x01 从任意 ...

  3. clob字段超过4000转String类型

    上次提到listagg()和wm_concat()方法合并过的字段类型为clob,要是字段长度超过4000,直接使用to_char()方法转会报错. 解决方法可以在java代码中使用流的方式转化成字符 ...

  4. 23.python中的类属性和实例属性

    在上篇的时候,我们知道了:属性就是属于一个对象的数据或者函数,我们可以通过句点(.)来访问属性,同时 python 还支持在运作中添加和修改属性. 而数据变量,类似于: name = 'scolia' ...

  5. Bzoj2149拆迁队:cdq分治 凸包

    国际惯例的题面:我们考虑大力DP.首先重新定义代价为:1e13*选择数量-(总高度+总补偿).这样我们只需要一个long long就能维护.然后重新定义高度为heighti - i,这样我们能选择高度 ...

  6. spring boot学习总结(一)-- 基础入门 Hello,spring boot!

    写在最前 SpringBoot是伴随着Spring4.0诞生的: 从字面理解,Boot是引导的意思,因此SpringBoot帮助开发者快速搭建Spring框架: SpringBoot帮助开发者快速启动 ...

  7. mysql_提示 Lock wait timeout exceeded解决办法

    我的mysql报这个错 err=1205 - Lock wait timeout exceeded; try restarting transaction 利用 SHOW PROCESSLIST来查看 ...

  8. USB设备的插入和弹出的监听以及软弹出可移动媒体(如Windows的移除USB设备) .

    一.监听USB设备的插入和弹出 当USB设备插入或者弹出时,Windows会产生一条全局消息:WM_DEVICECHANGE 我们需要做的是,获得这条消息的wParam参数,如果为DBT_DEVICE ...

  9. 通过webbrowser控件获取验证码

    1.首先介绍下基本控件(拖控件大家都会,我就不一一介绍了),看下图: 2.添加MSHTML引用,步骤如下: 解决方案—右键“引用”—​添加引用—在.NET下找到Microsoft.mshtml组件—点 ...

  10. Revit API画垂直于风管的风管

    start /// <summary> /// 选择风管与风管外一点,画与风管垂直的风管. /// </summary> [Transaction(TransactionMod ...