72.Edit Distance---dp
题目链接:https://leetcode.com/problems/edit-distance/description/
题目大意:找出两个字符串之间的编辑距离(每次变化都只消耗一步)。
法一(借鉴):经典dp。代码如下(耗时15ms):
//dp公式:dp[i][j]表示第一个字符串前i个字符到第二个字符串前j个字符的编辑距离长度
//当word1[i]==word2[j]时,dp[i][j]=dp[i-1][j-1]
//否则,dp[i][j]=min(dp[i-1][j],dp[i][j-1],dp[i-1][j-1])+1
public int minDistance(String word1, String word2) {
int len1 = word1.length(), len2 = word2.length();
int dp[][] = new int[len1+1][len2+1];
//初始化
for(int i = 0; i <= len1; i++) {
dp[i][0] = i;
}
for(int i = 0; i <= len2; i++) {
dp[0][i] = i;
}
for(int i = 1; i <= len1; i++) {//下标从1开始
for(int j = 1; j <= len2; j++) {
if(word1.charAt(i - 1) == word2.charAt(j - 1)) {
dp[i][j] = dp[i - 1][j - 1];
}
else {
int min = Integer.MAX_VALUE;
if(min > dp[i - 1][j - 1]) {
min = dp[i - 1][j - 1];
}
if(min > dp[i][j - 1]) {
min = dp[i][j - 1];
}
if(min > dp[i - 1][j]) {
min = dp[i - 1][j];
}
dp[i][j] = min + 1;
}
}
}
return dp[len1][len2];
}
dp数组变化(例子:abc到acde的编辑距离):
| 0 | 1("a") | 2("c") | 3("d") | 4("e") |
| 1("a") | 0(a->a) | 1(a->ac) | 2(a->acd) | 3(a->acde) |
| 2("b") | 1(ab->a) | 1(ab->ac) | 2(ab->acd) | 3(ab->acde) |
| 3("c") | 2(abc->a) | 1(abc->ac) | 2(abc->acd) | 3(abc->acde) |
从上表可清楚看见最后结果在dp[3][4]中。
dp数组填充顺序:从左上到右下,即每一次数值计算都要用到左边,上边,左上的数据。
72.Edit Distance---dp的更多相关文章
- 【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 编辑距离
Given two words word1 and word2, find the minimum number of operations required to convert word1 to ...
- 72. Edit Distance
题目: Given two words word1 and word2, find the minimum number of steps required to convert word1 to w ...
- [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(困难,确实挺难的,但很经典,双序列DP问题)
Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2 ...
- 72. Edit Distance (String; DP)
Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2 ...
- [leetcode DP]72. Edit Distance
计算最少用多少不把word1变为word2, 思路:建立一个dp表,行为word1的长度,宽为word2的长度 1.边界条件,dp[i][0] = i,dp[0][j]=j 2.最优子问题,考虑已经知 ...
- 第十八周 Leetcode 72. Edit Distance(HARD) O(N^2)DP
Leetcode72 看起来比较棘手的一道题(列DP方程还是要大胆猜想..) DP方程该怎么列呢? dp[i][j]表示字符串a[0....i-1]转化为b[0....j-1]的最少距离 转移方程分三 ...
随机推荐
- 51nod 1821 最优集合(思维+单调队列)
题意:一个集合S的优美值定义为:最大的x,满足对于任意i∈[1,x],都存在一个S的子集S',使得S'中元素之和为i. 给定n个集合,对于每一次询问,指定一个集合S1和一个集合S2,以及一个数k,要求 ...
- PHP 中数组获取不到元素
早上看到 SO 上一个有关 PHP 的问题,提问者描述有一个数组,使用 print_r 可以看到索引 key 和相对应的 value 都是存在的,但是访问该元素,不管是使用 array[key] 还是 ...
- elasticsearch 第二篇(配置篇)
配置 在es启动之前可以通过设置启动命令行启动参数.环境变量.文件等方式优化和配置es进行参数 环境变量 名称 示例 说明 ES_MIN_MEM 256M 用于配置java进程分配的最小内存 ES_M ...
- Linux(三)高级文本处理命令
一.cut (cut 命令可以从一个文本文件或者文本流中提取文本列 ) 1.cut语法 cut -d '分隔字符' -f fields 用于有特定分隔字符 cut -c 字符区间 ...
- python基础----继承与派生、组合、接口与归一化设计、抽象类、子类中调用父类方法
一.什么是继承 继承是一种创建新的类的方式,在pyth ...
- vue2.0 安装及项目搭建(一)
基本环境安装 1.安装node:从node.js官网下载并安装node.测试:win+R(打开命令行)-------输入cmd-------敲入node -v.如果出现相应版本号,即安装成功: 2.测 ...
- 简单shell 编程
简单shell编程 by dreamboy #!/bin/bash while true do echo clear echo echo " 系统维护菜单 " echo &qu ...
- web项目中解决post乱码和get乱码的方法
前提复习编码问题产生的原因: 1. 什么是URL编码. URL编码是一种浏览器用来打包表单输入的格式,浏览器从表单中获取所有的name和其对应的value,将他们以name/value编码方式作为U ...
- C++并发编程 异步任务
C++并发编程 异步任务 异步任务 std::async (1) std::async 会返回一个 std::future 对象, 这个对象持有最终计算出来的结果. 当需要这个值时, 只需要调用对象的 ...
- discuz安装,uc_server目录下乱码问题:
uc_server是gbk格式的情况,放服务器上,国外服务器环境可能会乱码百度检索的语句很重要,一开始,找vim编辑文本格式的问题,后来想批量解决服务器上文件格式~~~后百度:有没有其他人安装uc_s ...