LeetCode--072--编辑距离(python)
给定两个单词 word1 和 word2,计算出将 word1 转换成 word2 所使用的最少操作数 。
你可以对一个单词进行如下三种操作:
插入一个字符
删除一个字符
替换一个字符
示例 1:
输入: word1 = "horse", word2 = "ros"
输出: 3
解释:
horse -> rorse (将 'h' 替换为 'r')
rorse -> rose (删除 'r')
rose -> ros (删除 'e')
示例 2:
输入: word1 = "intention", word2 = "execution"
输出: 5
解释:
intention -> inention (删除 't')
inention -> enention (将 'i' 替换为 'e')
enention -> exention (将 'n' 替换为 'x')
exention -> exection (将 'n' 替换为 'c')
exection -> execution (插入 'u')


class Solution:
def minDistance(self, string1: str, string2: str) -> int:
m = len(string1)
n = len(string2)
f = [[None for _ in range(n+1)] for i in range(m+1)]
for i in range (m+1):
for j in range(n+1):
if i== 0:
f[i][j]=j
continue
if j == 0:
f[i][j] = i
continue
#insert delete replace
if string1[i-1] == string2[j-1]:
f[i][j]=f[i-1][j-1]
else:
f[i][j] = min(f[i-1][j],f[i][j-1],f[i-1][j-1]) + 1
return f[-1][-1]
参考 b站
LeetCode--072--编辑距离(python)的更多相关文章
- [LeetCode]题解(python):125 Valid Palindrome
题目来源 https://leetcode.com/problems/valid-palindrome/ Given a string, determine if it is a palindrome ...
- [LeetCode]题解(python):120 Triangle
题目来源 https://leetcode.com/problems/triangle/ Given a triangle, find the minimum path sum from top to ...
- [LeetCode]题解(python):119 Pascal's Triangle II
题目来源 https://leetcode.com/problems/pascals-triangle-ii/ Given an index k, return the kth row of the ...
- [LeetCode]题解(python):118 Pascal's Triangle
题目来源 https://leetcode.com/problems/pascals-triangle/ Given numRows, generate the first numRows of Pa ...
- [LeetCode]题解(python):116 Populating Next Right Pointers in Each Node
题目来源 https://leetcode.com/problems/populating-next-right-pointers-in-each-node/ Given a binary tree ...
- [LeetCode]题解(python):114 Flatten Binary Tree to Linked List
题目来源 https://leetcode.com/problems/flatten-binary-tree-to-linked-list/ Given a binary tree, flatten ...
- [LeetCode]题解(python):113 Path Sum II
题目来源 https://leetcode.com/problems/path-sum-ii/ Given a binary tree and a sum, find all root-to-leaf ...
- [LeetCode]题解(python):112 Path Sum
题目来源 https://leetcode.com/problems/path-sum/ Given a binary tree and a sum, determine if the tree ha ...
- [LeetCode]题解(python):111 Minimum Depth of Binary Tree
题目来源 https://leetcode.com/problems/minimum-depth-of-binary-tree/ Given a binary tree, find its minim ...
- [LeetCode]题解(python):110 Balanced Binary Tree
题目来源 https://leetcode.com/problems/balanced-binary-tree/ Given a binary tree, determine if it is hei ...
随机推荐
- 阶段3 1.Mybatis_12.Mybatis注解开发_3 mybatis注解开发保存和更新功能
使用直接来实现CRUD操作 Insert方法 创建测试类 把变量都定义在外面 写测试方法 修改链接的数据库 update方法 再加上address 被更新的数据
- 大sd卡 裂开了,写保护掉了。重新装好后,被写保护的解决办:
大sd卡 裂开了,写保护掉了.重新装好后,被写保护的解决办: 1.用烙铁把写保护附近的塑料往外顶一点点,就ok 别太热,也别劲太大.容易过,不能破坏原来的部分. 解决问题. 总结: 写保护,就是检 ...
- 获取win10壁纸
执行命令会将所有壁纸拷贝到桌面上的wallpaper文件夹内 bat xcopy %LOCALAPPDATA%\Packages\Microsoft.Windows.ContentDeliveryMa ...
- 浅谈 JVM 结构体系、类加载、JDK JRE JVM 三者的关系
一.java类,创建.编译.到运行的工程: 1.随便建一个Java类,保存后就是一个.java文件, 2.然后我们使用 javac命令编译 .java文件,生产 .class文件. 3.再然后使用 j ...
- Marked Ancestor
一道并查集的题目硬是被我当成线段树写了,感觉这样写虽然不是最好的,不过能a就行 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=103906 ...
- Hibernate-Criteria学习笔记
hibernate_jpa注解 目前最新版的hibernate,5.2,底层整合了jpa,用idea的hibernate工具生成实体时,实体包含了注解的配置文件,缺一不可 如,用户类实体,生成之后是这 ...
- [BZOJ 4332] [JSOI2012]分零食(DP+FFT)
[BZOJ 4332] [JSOI2012]分零食(DP+FFT) 题面 同学们依次排成了一列,其中有A位小朋友,有三个共同的欢乐系数O,S和U.如果有一位小朋友得到了x个糖果,那么她的欢乐程度就是\ ...
- PHP数据结构基本概念
原文:https://www.cnblogs.com/crystaltu/p/6408484.html 学习任何一种技术都应该先清楚它的基本概念,这是学习任何知识的起点!本文是讲述数据结构的基本概念, ...
- tensorflow学习笔记七----------卷积神经网络
卷积神经网络比神经网络稍微复杂一些,因为其多了一个卷积层(convolutional layer)和池化层(pooling layer). 使用mnist数据集,n个数据,每个数据的像素为28*28* ...
- python day2-爬虫实现github登录
GitHub登录 分析登录页面 开发者工具分析请求 从session请求分析得知: 1.请求的URL为:https://github.com/session 2.该请求为post请求,即需要上传dat ...