标签:动态规划

描述:

Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2. (each operation is counted as 1 step.)

You have the following 3 operations permitted on a word:

  • Insert a character
  • Delete a character
  • Replace a character

解题思路:

这一题做的很快,与之前的字符串匹配问题一样,在word1与word2的两个向量上分解字符串,同时遍历两个字符串:分别在两个子串中进行比较,

对于子串中拥有相同的字符的情况下:加入这个字符与不加入这个字符的意义是相同的,不会有更多的变化,所以有公式:

dp[i][j] = dp[i-1][j-1]

当两个子串的匹配的字符不同的情况下:有三种情况可以达到当前状态,修改1位,删除1位,加入1位,三个分别的位置为dp[i-1][j], dp[i][j-1],dp[i-1][j-1]

且三个位置记录着之前状态下,所存在的最小变化情况。所以要在当前状态下达到最小,则需要在之前最小的情况下加上1,所以公式有:

dp[i][j] = min(dp[i-1][j-1], dp[i-1][j], dp[i][j-1])+1

参考代码:

 public int minDistance(String word1, String word2) {
// write your code here
int[][] dp = new int[word1.length()+1][word2.length()+1]; for(int i = 0; i<=word1.length(); i++){
dp[i][0] = i;
} for(int j = 0; j<=word2.length(); j++){
dp[0][j] = j;
} for(int i=1; i<= word1.length(); i++){
for(int j=1; j<=word2.length(); 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[word1.length()][word2.length()]; }

LintCode刷题笔记-- Edit distance的更多相关文章

  1. 刷题72. Edit Distance

    一.题目说明 题目72. Edit Distance,计算将word1转换为word2最少需要的操作.操作包含:插入一个字符,删除一个字符,替换一个字符.本题难度为Hard! 二.我的解答 这个题目一 ...

  2. lintcode刷题笔记(一)

    最近开始刷lintcode,记录下自己的答案,数字即为lintcode题目号,语言为python3,坚持日拱一卒吧... (一). 回文字符窜问题(Palindrome problem) 627. L ...

  3. LintCode刷题笔记-- LongestCommonSquence

    标签:动态规划 题目描述: Given two strings, find the longest common subsequence (LCS). Your code should return ...

  4. LintCode刷题笔记-- PaintHouse 1&2

    标签: 动态规划 题目描述: There are a row of n houses, each house can be painted with one of the k colors. The ...

  5. LintCode刷题笔记-- Maximum Product Subarray

    标签: 动态规划 描述: Find the contiguous subarray within an array (containing at least one number) which has ...

  6. LintCode刷题笔记-- Maximal Square

    标签:动态规划 题目描述: Given a 2D binary matrix filled with 0's and 1's, find the largest square containing a ...

  7. LintCode刷题笔记-- Distinct Subsequences

    标签:动态规划 题目描述: Given a string S and a string T, count the number of distinct subsequences of T in S. ...

  8. LintCode刷题笔记-- BackpackIV

    标签: 动态规划 描述: Given an integer array nums with all positive numbers and no duplicates, find the numbe ...

  9. LintCode刷题笔记-- BackpackII

    标记: 动态规划 问题描述: Given n items with size Ai, an integer m denotes the size of a backpack. How full you ...

随机推荐

  1. HTML给div设置百分比高度无效的解决方式 - 库塔姆斯 - CSDN博客

    原文:HTML给div设置百分比高度无效的解决方式 - 库塔姆斯 - CSDN博客 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/HobHunter ...

  2. 工业派-配置Intel神经计算棒二代(NCS2)

    最近两天在工业派ubuntu16.04上配置了Intel神经计算棒二代——Intel Neural Compute Stick,配置过程之艰辛我都不想说了,实在是太折磨人.不过历尽千辛万苦,总算让计算 ...

  3. 利用jQuery获取jsonp

    前端js代码: $.ajax({ url: 'http://localhost:8080/webApp/somejsonp', dataType: "jsonp", jsonp: ...

  4. [编织消息框架][JAVA核心技术]动态代理应用4-annotationProcessor

    基础部份: 接下来讲编译JAVA时,生成自定义class 我们用 javax.annotation.processing.AbstractProcessor 来处理 public abstract c ...

  5. dubbo入门学习(四)-----dubbo配置

    配置来源 首先,从Dubbo支持的配置来源说起,默认有四种配置来源: JVM System Properties,-D参数 Externalized Configuration,外部化配置 Servi ...

  6. line-height:2和line-height:2em的区别,它们是有区别的

    line-height:2是2倍的意思,如果内部有不同大小文字的情况下,以最大文字为倍数. line-height:2em也是2倍文字大小的意思,但如果内部有大文字,它还是会以父容 器的大小来计算. ...

  7. JSP - (Java Server Pages) - Java服务器界面

    JSP简介: 在HTML中嵌入Java脚本代码,由应用服务器中的JSP引擎来编译和执行嵌入的Java脚本代码,然后将生成的整个页面信息返回给客户端: 一个JSP页面包含:静态内容(HTML静态文本), ...

  8. python-基础-基础知识-变量-选择-循环

    1 基础知识 1.1 注释的分类 1.2 变量以及类型 变量定义 num1 = 100 #num1就是一个变量,就好一个小菜篮子 num2 = 87 #num2也是一个变量 result = num1 ...

  9. Gym - 102082G

    Gym - 102082Ghttps://vjudge.net/problem/2198225/origin对于数列中任意一个数,要么从最左边到它不递减,要么从最右边到到它不递减,为了满足这个条件,就 ...

  10. 安装office2016时弹出microsoft setup bootstrapper已停止工作的解决办法

    安装office2016时安装进度条走到最后又回滚,弹出microsoft setup bootstrapper已停止工作,最后“安装出错” 经过了1天的试尽了各种控制面板卸载.文件夹删除.offic ...