题目描述:

  给出两个单词word1和word2,计算出将word1 转换为word2的最少操作次数。

  你总共三种操作方法:

  • 插入一个字符
  • 删除一个字符
  • 替换一个字符
样例
  给出 work1="mart" 和 work2="karma"

  返回 3

 public class Solution {
/**
* @param word1 & word2: Two string.
* @return: The minimum number of steps.
*/
public int minDistance(String word1, String word2) {
// write your code here
int[][] dp = new int[word1.length()+1][word2.length()+1]; for(int i=1;i<=word1.length();i++)
dp[i][0] = i; for(int i=1;i<=word2.length();i++)
dp[0][i] = i; for(int i=0;i<word1.length();i++){
for(int j=0;j<word2.length();j++){
if(word1.charAt(i) == word2.charAt(j)){
dp[i+1][j+1] = Math.min(dp[i][j],Math.min(dp[i][j+1],dp[i+1][j])+1);
}else{
dp[i+1][j+1] = Math.min(dp[i][j+1]+1,Math.min(dp[i][j],dp[i+1][j])+1);
}
}
}
return dp[word1.length()][word2.length()];
}
}

LintCode-编辑距离的更多相关文章

  1. lintcode:最小编辑距离

    最小编辑距离 给出两个单词word1和word2,计算出将word1 转换为word2的最少操作次数. 你总共三种操作方法: 插入一个字符 删除一个字符 替换一个字符 样例 给出 work1=&quo ...

  2. (lintcode全部题目解答之)九章算法之算法班题目全解(附容易犯的错误)

    --------------------------------------------------------------- 本文使用方法:所有题目,只需要把标题输入lintcode就能找到.主要是 ...

  3. Lintcode--008(编辑距离)

    http://www.lintcode.com/en/problem/edit-distance/ 2016-08-29 给出两个单词word1和word2,计算出将word1 转换为word2的最少 ...

  4. [LintCode]——目录

    Yet Another Source Code for LintCode Current Status : 232AC / 289ALL in Language C++, Up to date (20 ...

  5. [LeetCode] One Edit Distance 一个编辑距离

    Given two strings S and T, determine if they are both one edit distance apart. 这道题是之前那道Edit Distance ...

  6. C#实现Levenshtein distance最小编辑距离算法

    Levenshtein distance,中文名为最小编辑距离,其目的是找出两个字符串之间需要改动多少个字符后变成一致.该算法使用了动态规划的算法策略,该问题具备最优子结构,最小编辑距离包含子最小编辑 ...

  7. 利用Levenshtein Distance (编辑距离)实现文档相似度计算

    1.首先将word文档解压缩为zip /** * 修改后缀名 */ public static String reName(String path){ File file=new File(path) ...

  8. Lintcode 85. 在二叉查找树中插入节点

    -------------------------------------------- AC代码: /** * Definition of TreeNode: * public class Tree ...

  9. Lintcode 166. 主元素

    ----------------------------------- Moore's voting algorithm算法:从一个集合中找出出现次数半数以上的元素,每次从集合中去掉一对不同的数,当剩 ...

  10. Lintcode 166. 链表倒数第n个节点

    ----------------------------------- 最开始的想法是先计算出链表的长度length,然后再从头走 length-n 步即是需要的位置了. AC代码: /** * De ...

随机推荐

  1. Spring——jar包详解

    org.springframework.aop ——Spring的面向切面编程,提供AOP(面向切面编程)的实现 org.springframework.asm——spring 2.5.6的时候需要a ...

  2. MongoDB 启动异常

    今天启动MongoDB遇到异常状况 mongodb warning: 32-bit servers don't have journaling enable 解决方法: 删除数据库目录的.lock文件 ...

  3. 如何给Ubuntu 安装Vmware Tools

    http://jingyan.baidu.com/article/3065b3b6e8dedabecff8a435.html

  4. 输出多行字符的一个简单JAVA小程序

    public class JAVA { public static void main(String[] args) { System.out.println("-------------- ...

  5. BZOJ 3669: [Noi2014]魔法森林( LCT )

    排序搞掉一维, 然后就用LCT维护加边MST. O(NlogN) ------------------------------------------------------------------- ...

  6. php小数点后取两位的三种实现方法

    php小数点后取两位的方法. 方法一.经常用到小数点后取几位,但不能进位的情况. 比如3.149569取小数点后两位,最后两位不能四舍五入.结果:3.14. 可以使用函数floor. 该函数是舍去取整 ...

  7. python xlrd对excel的读取功能

    工作簿 xlrd.open_workbook('test.xls') workbook.dump() workbook.nsheets workbook.sheets() workbook.sheet ...

  8. Use eplipse to develop Python project

    Source: This is the example how to use eclipse and python. http://www.360doc.com/content/15/0206/10/ ...

  9. Visual Studio 常用快捷键总结

    删除或剪切一行:  Ctrl + X 或者 Shift+Delete格式化整个文档:  Ctrl + K + D 或者 Ctrl+E+D智能感知: Ctrl + J 或者 Alt+→折叠所有方法: C ...

  10. MySQL mysqlimport 从txt文件中导入数据到mysql数据库

    mysqlimport: 我说这个我们还是先从世界观方法论的高度来理解一下便有更加准确的把握.数据导入不外呼有两个部分 第一部分:目标对象--我们要把数据导给谁(mysqlimport 的目标对象自然 ...