题目来源:

  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. ReactNative实现通知监听事件

    事例1: 只在rn里面发送和接受消息. A界面: import {DeviceEventEmitter} from 'react-native'; //... componentDidMount(){ ...

  2. android高仿微信拍照、多选、预览、删除(去除相片)相冊功能

    先声明授人与鱼不如授人与渔,仅仅能提供一个思路,当然须要源代码的同学能够私下有偿问我要源代码:QQ:508181017 工作了将近三年时间了,一直没正儿八经的研究系统自带的相冊和拍照,这回来个高仿微信 ...

  3. Objective-C 类属性和方法的訪问权限

    OC中提供了4种訪问权限.@private, @public, @protected这三种和其它的C++, Java是一样的,@package这个訪问权限并非Java里的包訪问权限,OC中没有包的概念 ...

  4. 关于Apacheserver的訪问控制

    Apache的訪问控制指对不论什么资源的不论什么方式的訪问控制. 一.基于主机或者IP地址的控制 这样的訪问控制基于訪问者的主机名或者IP地址,通过使用 Deny 和 Allow 指令.实现同意或者禁 ...

  5. gulp脚本编写方法

    建立一个gulpfile.js文件,内容直接抄gulp-htmlmin的readme: var gulp = require('gulp'); var htmlmin = require('gulp- ...

  6. iOS 点转成字符串,再字符串转换成点

    CGPointFromString(<#NSString *string#>) NSStringFromCGPoint(<#CGPoint point#>)

  7. InnoDB的配置

    http://www.cnblogs.com/szx_rencaijob/archive/2010/04/28/1723211.html 推荐InnoDB的配置(1G内存情况,主要运行mysql服务器 ...

  8. 有关std::map和std::vector的使用

    先说map吧. 最需要注意的就是:用下标访问map中的元素时,与使用下标访问vector的行为截然不同! 用下标访问不存在的元素时,将导致在map容器中添加一个新的元素,它的键即为该下标! 然而很多时 ...

  9. poj2987 Firing

    以前只是A过很简单的最大闭合权像hdu1565之类,完全的最大流模板题.但是都完全不太懂最大闭合权的定义及其用途. 关于最大流的基础知识,大家可以自己网上搜索关键字.有点基础的哥们妹们,推荐看看胡伯涛 ...

  10. JavaScript 入門

    <html lang="en"> <head>   <meta charset="UTF-8">   <meta na ...