[leetcode]Edit Distance @ Python
原题地址: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的更多相关文章
- [LeetCode] Edit Distance 编辑距离
Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2 ...
- Leetcode:Edit Distance 解题报告
Edit Distance Given two words word1 and word2, find the minimum number of steps required to convert ...
- [LeetCode] 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 steps required to convert word1 to word2 ...
- [LeetCode] Edit Distance(很好的DP)
Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2 ...
- LeetCode: Edit Distance && 子序列题集
Title: Given two words word1 and word2, find the minimum number of steps required to convert word1 t ...
- LeetCode——Edit Distance
Question Given two words word1 and word2, find the minimum number of steps required to convert word1 ...
- [LeetCode] One Edit Distance 一个编辑距离
Given two strings S and T, determine if they are both one edit distance apart. 这道题是之前那道Edit Distance ...
- 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 ...
随机推荐
- BZOJ2090 : [Poi2010]Monotonicity 2
设f[i]表示以i为结尾的最长的合法序列的长度,=号直接维护,<号和>号用两棵树状数组维护即可,时间复杂度$O(n\log n)$. #include<cstdio> #def ...
- 【GDKOI 2016】地图 map 类插头DP
Description 对于一个n*m的地图,每个格子有五种可能:平地,障碍物,出口,入口和神器.一个有效的地图必须满足下列条件: 1.入口,出口和神器都有且仅出现一次,并且不在同一个格子内. 2.入 ...
- Android 9 patch 图片 (.9.png 格式图片) 的特点和制作(转)
本文围绕 .9.png 格式图片讨论以下两个话题: 1. 该格式图片的特点 2. 制作方式 一 .9.png 格式的文件的特点 与传统的png 格式图片相比, 9.png 格式图片在图片四周有一圈一个 ...
- 调试工具BTrace 的使用--例子
http://www.cnblogs.com/serendipity/archive/2012/05/14/2499840.html
- dell T420热插拔安装过程
http://v.youku.com/v_show/id_XNTUzMjk4NTQw.html
- Win7电脑开启局域网连接和共享过程中出现的"您可能没有权限使用网络资源"的解决办法
Win7电脑开启局域网连接和共享 http://bbs.ithome.com/thread-334567-1-1.html http://jingyan.baidu.com/article/6dad5 ...
- 初识序列化和反序列化,使用BinaryFormatter类、ISerializable接口、XmlSerializer类进行序列化和反序列化
序列化是将对象转换成字节流的过程,反序列化是把字节流转换成对象的过程.对象一旦被序列化,就可以把对象状态保存到硬盘的某个位置,甚至还可以通过网络发送给另外一台机器上运行的进程.本篇主要包括: ● 使用 ...
- C#编程(二十六)----------泛型
泛型 有了泛型,就可以创建独立于被包含类型的类和方法了.我们不必给不同的类型编写功能相同的许多方法或类,只创建一个方法或类即可. 另一个减少代码的选项是使用object类,但object类不是类型安全 ...
- 基于Memcached的tomcat集群session共享所用的jar
多个tomcat各种序列化策略配置如下:一.java默认序列化tomcat配置conf/context.xml添加<Manager className="de.javakaffee.w ...
- Spring Websocket实现文本、图片、声音、文件下载及推送、接收及显示(集群模式)
相关环境 Nginx,Spring5.x当前(要选择4.0+),tomcat8.x,Quartz 2.x集群(实际运用是Quartz的集群模式和单机模式共存的) 测试面页:http://sms.rey ...