72. Edit Distance(编辑距离 动态规划)
You have the following 3 operations permitted on a word:
- Insert a character
- Delete a character
- Replace a character
Example 1:
Input: word1 = "horse", word2 = "ros"
Output: 3
Explanation:
horse -> rorse (replace 'h' with 'r')
rorse -> rose (remove 'r')
rose -> ros (remove 'e')
Example 2:
Input: word1 = "intention", word2 = "execution"
Output: 5
Explanation:
intention -> inention (remove 't')
inention -> enention (replace 'i' with 'e')
enention -> exention (replace 'n' with 'x')
exention -> exection (replace 'n' with 'c')
exection -> execution (insert 'u')
class Solution: def minDistance(self, x, y):
"""
:type word1: str
:type word2: str
:rtype: int
"""
m = len(x)
n = len(y)
dp = [[' '] * (n + 1) for i in range(m + 1)] for i in range(m + 1):
dp[i][0] = i
for j in range(n + 1):
dp[0][j] = j for i in range(1, m + 1):
for j in range(1, n + 1):
if x[i - 1] == y[j - 1]:
dp[i][j] = dp[i - 1][j - 1]
else:
dp[i][j] = 1 + min(dp[i - 1][j], dp[i]
[j - 1], dp[i - 1][j - 1])
return dp[m][n]
72. Edit Distance(编辑距离 动态规划)的更多相关文章
- [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 (编辑距离) 解题思路和方法
Edit Distance Given two words word1 and word2, find the minimum number of steps required to convert ...
- 【LeetCode】72. Edit Distance 编辑距离(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 记忆化搜索 动态规划 日期 题目地址:http ...
- 【Leetcode】72 Edit Distance
72. Edit Distance Given two words word1 and word2, find the minimum number of steps required to conv ...
- 刷题72. Edit Distance
一.题目说明 题目72. Edit Distance,计算将word1转换为word2最少需要的操作.操作包含:插入一个字符,删除一个字符,替换一个字符.本题难度为Hard! 二.我的解答 这个题目一 ...
- [LeetCode] 72. Edit Distance(最短编辑距离)
传送门 Description Given two words word1 and word2, find the minimum number of steps required to conver ...
- [LeetCode] Edit Distance 编辑距离
Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2 ...
- Edit Distance编辑距离(NM tag)- sam/bam格式解读进阶
sam格式很精炼,几乎包含了比对的所有信息,我们平常用到的信息很少,但特殊情况下,我们会用到一些较为生僻的信息,关于这些信息sam官方文档的介绍比较精简,直接看估计很难看懂. 今天要介绍的是如何通过b ...
- 72. Edit Distance
题目: Given two words word1 and word2, find the minimum number of steps required to convert word1 to w ...
随机推荐
- 判断app是否是第一次启动
如何判断app是否是第一启动呢,第一次启动的时候,加载引导页面等, 一下是我的解决方案: 1.在"AppDelegate.h"中找到- (BOOL)application:(UIA ...
- Python 爬虫知识点 - 淘宝商品检索结果抓包分析(续一)
通过前一节得出地址可能的构建规律,如下: https://s.taobao.com/search?data-key=s&data-value=44&ajax=true&_ksT ...
- Nginx(四)-- 配置文件之location
1.location的作用 location主要做定位功能,根据uri来进行不同的定位. 2.location的语法 location [=|~|~*|^~] /uri/ { …} = 开头表示精确匹 ...
- 实例:用类来写一个 memcached 启动脚本
[root@localhost ~]$ yum install -y memcached #!/usr/bin/env python #-*- coding:utf-8 -*- import os i ...
- C语言简介(转自菜鸟教程)
C 简介 C 语言是一种通用的高级语言,最初是由丹尼斯·里奇在贝尔实验室为开发 UNIX 操作系统而设计的.C 语言最开始是于 1972 年在 DEC PDP-11 计算机上被首次实现. 在 1978 ...
- Sass-学习笔记【基础篇】
最下边附结构图 在线编辑器网址如下:http://sassmeister.com/ 注意编写的时候,符号千万别用了中文的:.:.....之类的,会报错,Sass也转换不成css. less和sass ...
- 《IT项目经理成长手记》
下载地址: https://download.csdn.net/download/h623691640/9109265 读后感: 曾担任过试用期3个月项目经理,遇到的问题确实如书中所说,几乎有一些是一 ...
- C# 关于调用office com组件导出Excel
服务器环境: 环境为win2008 r2,系统为64位,程序是C#的winform.因为需要处理数据,然后生成Excel,耗时太长,就使用了多线程.winform程序是由计划任务启动,每天晚上去跑. ...
- 【BZOJ3529】[Sdoi2014]数表 莫比乌斯反演+树状数组
[BZOJ3529][Sdoi2014]数表 Description 有一张N×m的数表,其第i行第j列(1 < =i < =礼,1 < =j < =m)的数值为能同时整除i和 ...
- DES加密解密 Java中运用
DES全称Data Encryption Standard,是一种使用密匙加密的块算法.现在认为是一种不安全的加密算法,因为现在已经有用穷举法攻破DES密码的报道了.尽管如此,该加密算法还是运用非常普 ...