题目来源:

  https://leetcode.com/problems/edit-distance/


题意分析:

  word1最少通过多少步可以变成word2。word1只能进行一下的操作。a)插入一个字符,b)删除一个字符,c)替代一个字符。比如“aba”变成“abc”只需要通过替代最后一个字符就可以达到。


题目思路:

  这很明显是一个动态规划的问题。建立一个二维数组ans,其中ans[i][j]代表word1[i:]要变成word2[j:]至少需要多少步。那么如果word1[i] == word2[j],则ans[i][j] = ans[i+1][j+1],否者,ans[i][j]= min(ans[i +1][j],ans[ans[i][j+1],ans[i+1][j+1])/分别代表每个操作/ + 1。只要处理好初始化问题就可以了。


代码(Python):

  

class Solution(object):
def minDistance(self, word1, word2):
"""
:type word1: str
:type word2: str
:rtype: int
"""
m,n = len(word1),len(word2)
ans = [[0 for i in range(n + 1)] for j in range(m + 1)]
for i in range(m + 1):
ans[i][n] = m - i
for i in range(n + 1):
ans[m][i] = n - i
m -= 1;n -= 1
while m >= 0:
t = n
while t >= 0:
if word1[m] == word2[t]:
ans[m][t] = ans[m + 1][t + 1]
else:
ans[m][t] = min(ans[m][t+1],ans[m+1][t],ans[m+1][t+1]) + 1
t -= 1
m -= 1
return ans[0][0]

转载请注明出处:http://www.cnblogs.com/chruny/p/5069714.html

[LeetCode]题解(python):072-Edit Distance的更多相关文章

  1. 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 ...

  2. 【LeetCode】161. One Edit Distance

    Difficulty: Medium  More:[目录]LeetCode Java实现 Description Given two strings S and T, determine if the ...

  3. 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 ...

  4. LeetCode(72) Edit Distance

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

  5. 072 Edit Distance 编辑距离

    给出两个单词 word1 和 word2,找出将 word1 转换成 word2 所使用的最少的步骤数 (每个操作记为一步).你可以对一个单词进行以下三种操作:a) 插入一个字符b) 删除一个字符c) ...

  6. LeetCode之“动态规划”:Edit Distance

    题目链接 题目要求: Given two words word1 and word2, find the minimum number of steps required to convert wor ...

  7. LeetCode One Edit Distance

    原题链接在这里:https://leetcode.com/problems/one-edit-distance/ Given two strings S and T, determine if the ...

  8. [Leetcode Week8]Edit Distance

    Edit Distance 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/edit-distance/description/ Description ...

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

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

  10. [LeetCode] Edit Distance 编辑距离

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

随机推荐

  1. Fibonacci(数论 输出前四位Fibonacci)

    Fibonacci Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total ...

  2. ActiveMQ使用STOMP协议的一个错误问题:Unexpected ACK received for message-id

    使用某些语言环境下的stomp包(比如php python ruby),可能会出现如下问题: Unexpected ACK received for message-id 这一般可能有两个原因. 1. ...

  3. SSH整合方案2

    [案例3]SSH整合_方案2 **  案例描述  两个知识点的演示  其一,SSH整合的第二个方案  其二,Spring+JDBC+Struts2  参考代码  31) 使用工程spring4  32 ...

  4. xcode UIImage图片拉伸

    图片拉伸 +(UIImage*)wlisWithImage:(NSString *)name{ //获取图片 UIImage * img=[UIImage imageNamed:name]; //获取 ...

  5. CSS:margin负数的使用

    给所有div加上边框=10px之后,再给所有div设置margin-left与margin-top;以及浮动(float:left) 因此时需要鼠标悬停效果:所以设置给div设置伪类:hover,然因 ...

  6. Asp.Net MVC 控制器

    原文链接:http://www.asp.net/learn/mvc/ 这篇教程探索了ASP.NET MVC控制器(controller).控制器动作(controller action)和动作结果(a ...

  7. IO库 8.2

    题目:编写一个测试函数,将cin作为参数传入. #include <iostream> using std::istream; istream& func(istream& ...

  8. 请求http服务

    ①服务方法 [HttpGet]//get服务 public JsonResult GetUserName(int id) { try { IXiao_UserBLL bll = new Xiao_Us ...

  9. 启动tomcat的时候,服务器暂停到装载mysql驱动文件的原因

    1.使用spring+mybatis,由于mybatis的配置文件中jdbc类型的错误使得,tomcat无法正常启动,在编写mybatis一定确保jdbc类型,java类型正确,jdbc类型要大写! ...

  10. 关于Google指令(别提baidu)

    关于google指令 关于google指令 google为我们准备好了的"指令"(directive),可以最大限度帮助我们完成每一次搜索.这些指令其实就是一个个关键字,能让我们从 ...