给出两个单词 word1 和 word2,找出将 word1 转换成 word2 所使用的最少的步骤数 (每个操作记为一步)。
你可以对一个单词进行以下三种操作:
a) 插入一个字符
b) 删除一个字符
c) 替换一个字符
详见:https://leetcode.com/problems/edit-distance/description/

Java实现:

class Solution {
public int minDistance(String word1, String word2) {
int m=word1.length();
int n=word2.length();
//dp[i][j]表示从word1的前i个字符转换到word2的前j个字符所需要的步骤
int[][] dp=new int[m+1][n+1];
//先给这个二维数组dp的第一行第一列赋值,因为第一行和第一列对应的总有一个字符串是空串,于是转换步骤完全是另一个字符串的长度。
for(int i=0;i<=m;++i){
dp[i][0]=i;
}
for(int j=0;j<=n;++j){
dp[0][j]=j;
}
//当word1[i] == word2[j]时,dp[i][j] = dp[i - 1][j - 1],其他情况时,dp[i][j]是其左,左上,上的三个值中的最小值加1
for(int i=1;i<=m;++i){
for(int j=1;j<=n;++j){
if(word1.charAt(i-1)==word2.charAt(j-1)){
dp[i][j]=dp[i-1][j-1];
}else{
dp[i][j]=Math.min(dp[i-1][j-1],Math.min(dp[i-1][j],dp[i][j-1]))+1;
}
}
}
return dp[m][n];
}
}

参考:http://www.cnblogs.com/grandyang/p/4344107.html

http://www.cnblogs.com/lihaozy/archive/2012/12/31/2840152.html

072 Edit Distance 编辑距离的更多相关文章

  1. Edit Distance编辑距离(NM tag)- sam/bam格式解读进阶

    sam格式很精炼,几乎包含了比对的所有信息,我们平常用到的信息很少,但特殊情况下,我们会用到一些较为生僻的信息,关于这些信息sam官方文档的介绍比较精简,直接看估计很难看懂. 今天要介绍的是如何通过b ...

  2. [LeetCode] Edit Distance 编辑距离

    Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2 ...

  3. 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 ...

  4. leetCode 72.Edit Distance (编辑距离) 解题思路和方法

    Edit Distance Given two words word1 and word2, find the minimum number of steps required to convert  ...

  5. [LeetCode] 72. Edit Distance 编辑距离

    Given two words word1 and word2, find the minimum number of operations required to convert word1 to  ...

  6. leetcode72. Edit Distance(编辑距离)

    以下为个人翻译方便理解 编辑距离问题是一个经典的动态规划问题.首先定义dp[i][j表示word1[0..i-1]到word2[0..j-1]的最小操作数(即编辑距离). 状态转换方程有两种情况:边界 ...

  7. 【LeetCode每天一题】Edit Distance(编辑距离)

    Given two words word1 and word2, find the minimum number of operations required to convert word1 to  ...

  8. 【LeetCode】72. Edit Distance 编辑距离(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 记忆化搜索 动态规划 日期 题目地址:http ...

  9. edit distance(编辑距离,两个字符串之间相似性的问题)

    Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2 ...

随机推荐

  1. Ubuntu 16.04上编译SkyEye的测试程序

    一.首先确保Ubuntu系统上已经安装了Skyeye.skyeye-testsuite和arm-linux-gcc交叉编译工具链,如果没有安装请参考: 1.Skyeye的安装:http://www.c ...

  2. HihoCoder1673 : 01间隔矩阵([Offer收割]编程练习赛41)(单调队列)

    描述 给定一个N × M的01矩阵,小Hi希望从中找到一个01间隔的子矩阵,并且子矩阵的面积越大越好. 例如对于 0101010 1000101 0101010 1010101 0101010 在右侧 ...

  3. 蓝桥杯训练 2n皇后问题

    给定一个n*n的棋盘,棋盘中有一些位置不能放皇后.现在要向棋盘中放入n个黑皇后和n个白皇后,使任意的两个黑皇后都不在同一行.同一列或同一条对角线上,任意的两个白皇后都不在同一行.同一列或同一条对角线上 ...

  4. ACM学习历程—HDU1719 Friend(数论)

    Description Friend number are defined recursively as follows. (1) numbers 1 and 2 are friend number; ...

  5. U盘安装CentOS的坑

    坑一:U盘安装盘配置的路径错误 采用UltraISO制作的CentOS7的安装光盘,但是在实体机安装的时候,发生了一些奇怪的失败,比如 dracut-initqueue timeout等,后来在网上搜 ...

  6. ReSIProcate源码目录下功能说明

    1.basicCall,basicMessage,basicRegister,resip_test测试用的 2.db_static Sleepy Cat开发的Berkeley DB 3.dum为Dia ...

  7. AR/VR-VR-Info-Micron-Insight:虚拟现实开辟心理健康新途径

    ylbtech-AR/VR-VR-Info-Micron-Insight:虚拟现实开辟心理健康新途径 1.返回顶部 1. 虚拟现实开辟心理健康新途径 全国心理疾病联盟最近发表的一份报告揭示了惊人的统计 ...

  8. jsonp实现数据跨域请求

    1.我们知道,哪怕跨域js文件中的代码(当然指符合web脚本安全策略的),web页面也是可以无条件执行的. 远程服务器remoteserver.com根目录下有个remote.js文件代码如下: al ...

  9. thinkpad取消fn键功能

    转自:https://bbs.thinkpad.com/thread-1834235-1-1.html 1就是一直觉得fn建自动开启很烦人,于是百度后得到 我们可以 控制面板 -- 键盘--think ...

  10. fastreport整理

    Q1:如何直接打印,不显示打印对话框? frxReport1.PrintOptions.ShowDialog := false; frxReport1.PrepareReport(true); frx ...