[LeetCode]题解(python):072-Edit Distance
题目来源:
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的更多相关文章
- 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 ...
- 【LeetCode】161. One Edit Distance
Difficulty: Medium More:[目录]LeetCode Java实现 Description Given two strings S and T, determine if the ...
- 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 ...
- LeetCode(72) Edit Distance
题目 Given two words word1 and word2, find the minimum number of steps required to convert word1 to wo ...
- 072 Edit Distance 编辑距离
给出两个单词 word1 和 word2,找出将 word1 转换成 word2 所使用的最少的步骤数 (每个操作记为一步).你可以对一个单词进行以下三种操作:a) 插入一个字符b) 删除一个字符c) ...
- LeetCode之“动态规划”:Edit Distance
题目链接 题目要求: Given two words word1 and word2, find the minimum number of steps required to convert wor ...
- LeetCode One Edit Distance
原题链接在这里:https://leetcode.com/problems/one-edit-distance/ Given two strings S and T, determine if the ...
- [Leetcode Week8]Edit Distance
Edit Distance 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/edit-distance/description/ Description ...
- [LeetCode] One Edit Distance 一个编辑距离
Given two strings S and T, determine if they are both one edit distance apart. 这道题是之前那道Edit Distance ...
- [LeetCode] Edit Distance 编辑距离
Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2 ...
随机推荐
- English - 英文写作中的最常见“十大句式”
英文写作中的最常见“十大句式” from 小木虫论坛 一.否定句 许多否定句不含not的否定结构.如果论文作者能正确使用他们,就会增加写作的闪光点,使文章显得生动活泼. 1.Instead of in ...
- 玩转kindle paperwhite: 如何越狱,安装强大外挂软件koreader
NOTICE 1: 在更新kpvbooklet和使用最新版本的koreader(v2013.03-211)时候,会出现pdf文档无法重排的错误.亲测. 如果你是使用的最新版本koreader且出现上述 ...
- oracle归档日志
前几天因为导入大的东西,弄得很久都没动静,一看最后才发现是归档满了.但是很多的命令还是很是很不熟悉,所以看了下,百度了下.整理下这个. 1.查看归档日志大小及使用情况 select * from v$ ...
- HTML与CSS绘制简单DIV布局
HTML代码<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF ...
- [Swust OJ 1139]--Coin-row problem
题目链接: http://acm.swust.edu.cn/contest/0226/problem/1139/ There is a row of n coins whose values are ...
- Problem F: Exponentiation大数求幂
DescriptionProblems involving the computation of exact values of very large magnitude and precision ...
- ORA-01045: user XXZY lacks CREATE SESSION privilege; logon denied
在创建用户时,一般我们都分配connect.dba.resource 角色,但是,为什么登陆时还报错呢 原因:用户角色没有激动 解决:ALTER USER XXXX DEFAULT ROLE &quo ...
- symfony2-不同bundle的entity的一对多关系
重点:其实和普通一个bundle中一样,只是把entity地址写全就行. 例子: 表commentone (多方) 表shopone(一方) 在Userbundle中的Commentone实体对应关系
- python使用mysql的三个模块:mysql.connector、sqlalchemy、MySQLdb
在python中使用mysql其实很简单,只要先安装对应的模块即可,那么对应的模块都有什么?官方也没指定也没提供,pcat就推荐自己遇到的3个模块:mysql.connector.sqlalchemy ...
- MySQL 设置数据库的隔离级别
在会话级别设置隔离级别 1.read commited :set session transaction isolation level read committed; 2.repeatable re ...