[leetcode DP]72. Edit Distance
计算最少用多少不把word1变为word2,
思路:建立一个dp表,行为word1的长度,宽为word2的长度
1.边界条件,dp[i][0] = i,dp[0][j]=j
2.最优子问题,考虑已经知道word1[0:i-1]转变为word2[0:j-1]的次数,只需要考虑word1[i]和word[j]的情况
3.子问题重叠,word1[i]和word2[j]是否相等,每种情况下怎样有最少步骤
class Solution(object):
def minDistance(self, word1, word2):
d1,d2 = len(word1),len(word2)
dp = [[0 for j in range(d2+1)] for i in range(d1+1)]
for i in range(d1+1):
dp[i][0] = i
for j in range(d2+1):
dp[0][j] = j
for i in range(1,d1+1):
for j in range(1,d2+1):
if word1[i-1] == word2[j-1]:
dp[i][j] = dp[i-1][j-1]
else:
dp[i][j] = min(dp[i][j-1],dp[i-1][j-1],dp[i-1][j])+1
return dp[d1][d2]
[leetcode DP]72. Edit Distance的更多相关文章
- 【Leetcode】72 Edit Distance
72. Edit Distance Given two words word1 and word2, find the minimum number of steps required to conv ...
- 【一天一道LeetCode】#72. Edit Distance
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given t ...
- 【LeetCode】72. Edit Distance 编辑距离(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 记忆化搜索 动态规划 日期 题目地址:http ...
- [leetcode]161. One Edit Distance编辑步数为一
Given two strings s and t, determine if they are both one edit distance apart. Note: There are 3 pos ...
- 刷题72. Edit Distance
一.题目说明 题目72. Edit Distance,计算将word1转换为word2最少需要的操作.操作包含:插入一个字符,删除一个字符,替换一个字符.本题难度为Hard! 二.我的解答 这个题目一 ...
- [LeetCode] 72. Edit Distance 编辑距离
Given two words word1 and word2, find the minimum number of operations required to convert word1 to ...
- [LeetCode] 72. Edit Distance(最短编辑距离)
传送门 Description Given two words word1 and word2, find the minimum number of steps required to conver ...
- leetCode 72.Edit Distance (编辑距离) 解题思路和方法
Edit Distance Given two words word1 and word2, find the minimum number of steps required to convert ...
- 72. Edit Distance
题目: Given two words word1 and word2, find the minimum number of steps required to convert word1 to w ...
随机推荐
- HDU 4707 Pet 邻接表实现
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4707 解题报告:题目大意是在无向图G中有n个点,分别从0 到n-1编号,然后在这些点之间有n-1条边, ...
- Android笔记之广播
为了容易理解,可以将广播代入到事件模型中,发送广播消息看做是触发event,BroadcastReceiver是处理事件的回调逻辑. 广播这种模型中涉及到两个角色,就是广播的发送者和接收者,所以会涉及 ...
- 说说C语言运算符的“优先级”与“结合性”
论坛和博客上常常看到关于C语言中运算符的迷惑,甚至是错误的解读.这样的迷惑或解读大都发生在表达式中存在着较为复杂的副作用时.但从本质上看,仍然是概念理解上的偏差.本文试图通过对三个典型表达式的分析,集 ...
- Django 基础命令
- leetcode之 两数之和
# -*- coding: utf-8 -*- # @Time : 2018/9/27 21:41 # @Author : cxa # @File : twonum.py # @Software: P ...
- 使用Cache缓存
存放位置:服务器内存,用于频繁访问且不轻易更改的内容缓存. string CacheKey = "CT1"; //检索指定项, object objModel = Cache.Ge ...
- Ubuntu 12.04下LVM2安装和操作实验
实验环境: VirtualBox v4.3.20 Lubuntu 12.04LTS 前期准备: 1.添加虚拟盘:菜单"控制"->"设置"->&quo ...
- [转]在C#程序设计中使用Win32类库
http://blog.163.com/j_yd168/blog/static/496797282008611326218/ C# 用户经常提出两个问题:“我为什么要另外编写代码来使用内置于 ...
- 002_Linux-Memory专题
一.单独查看某个进程的内存占用 pmap 736 | tail -n 1 二. 以前我对这块认识很模糊,而且还有错误的认识:今天由我同事提醒,所以我决定来好好的缕缕这块的关系. 图: -------- ...
- MAVLink v1.0详解——结构
本文针对 MAVLink v1.0版本,协议版本:3. MAVLink是为微型飞行器MAV(Micro Air Vehicle)设计的(LGPL)开源的通讯协议.是无人飞行器和地面站(Ground C ...