标签:动态规划

描述:

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. 《DSP using MATLAB》Problem 8.1

    代码: %% ------------------------------------------------------------------------ %% Output Info about ...

  2. JSON高亮格式化页面显示

    高亮CSS定义: <style type="text/css"> pre {outline: 1px solid #ccc; padding: 5px; margin: ...

  3. 轻量级IoC框架Ninject.NET搭建

    说在之前的话 IOC的概念相信大家比较熟悉了,习惯性称之为依赖注入或控制反转,园子里对基于MVC平台IOC设计模式已经相当多了,但大家都只知道应该怎么应用一个IOC模式,比如Ninject, Unit ...

  4. BMP 图片格式

     BMP根据颜色深度,可以分为2(1位).16(4位).256(8位).65536(16位)和1670万(24位)以及32位含有alpha通道.8位图像可以是 索引彩色图像外,也可以是灰阶图像,而索引 ...

  5. XStream JavaBean对象转换成XML!

    代码实例: 1.javaBean类: package com.hsinfo.web.Demo.XStream; public class City { private String name; pub ...

  6. Vue配置多个跨域目标链接

    参考: https://segmentfault.com/a/1190000016199721 1.通过使用的http-proxy-middleware来实现跨域代理 devServer: { dis ...

  7. 如何解决mysql服务器load高

    .登录主机 # ssh hostname .确定是否是mysql导致 # top .查看是哪些sql正在慢查询 # mysql -h hostname -P port -u username # sh ...

  8. JavaWeb工程中url地址的写法

    两种url地址: 1. "/"给服务器使用, 代表web工程根路径(webroot)2. "/"给浏览器使用, 代表tomcat 目录下的webapps文件夹 ...

  9. poj 1958

    传送门 四塔汉诺塔问题,转移方程非常玄学,f[i]=min(f[j]*2+d[i-j]) (1 <=j < i),d表示三塔下的汉诺塔问题,这个方程的意思是将j个在四塔模式下有A挪到B,然 ...

  10. anchor-free : CornerNet 和 CenterNet 简要笔记

    CornerNethourglass network -> prediction module = corner pooling -> heatmaps + embedding + off ...