LintCode刷题笔记-- Edit distance
标签:动态规划
描述:
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的更多相关文章
- 刷题72. Edit Distance
一.题目说明 题目72. Edit Distance,计算将word1转换为word2最少需要的操作.操作包含:插入一个字符,删除一个字符,替换一个字符.本题难度为Hard! 二.我的解答 这个题目一 ...
- lintcode刷题笔记(一)
最近开始刷lintcode,记录下自己的答案,数字即为lintcode题目号,语言为python3,坚持日拱一卒吧... (一). 回文字符窜问题(Palindrome problem) 627. L ...
- LintCode刷题笔记-- LongestCommonSquence
标签:动态规划 题目描述: Given two strings, find the longest common subsequence (LCS). Your code should return ...
- LintCode刷题笔记-- PaintHouse 1&2
标签: 动态规划 题目描述: There are a row of n houses, each house can be painted with one of the k colors. The ...
- LintCode刷题笔记-- Maximum Product Subarray
标签: 动态规划 描述: Find the contiguous subarray within an array (containing at least one number) which has ...
- LintCode刷题笔记-- Maximal Square
标签:动态规划 题目描述: Given a 2D binary matrix filled with 0's and 1's, find the largest square containing a ...
- LintCode刷题笔记-- Distinct Subsequences
标签:动态规划 题目描述: Given a string S and a string T, count the number of distinct subsequences of T in S. ...
- LintCode刷题笔记-- BackpackIV
标签: 动态规划 描述: Given an integer array nums with all positive numbers and no duplicates, find the numbe ...
- LintCode刷题笔记-- BackpackII
标记: 动态规划 问题描述: Given n items with size Ai, an integer m denotes the size of a backpack. How full you ...
随机推荐
- 《DSP using MATLAB》Problem 8.31
代码: %% ------------------------------------------------------------------------ %% Output Info about ...
- 使用scrapy框架来进行抓取的原因
在python爬虫中:使用requests + selenium就可以解决将近90%的爬虫需求,那么scrapy就是解决剩下10%的吗? 这个显然不是这样的,scrapy框架是为了让我们的爬虫更强大. ...
- [转]TabControl Style in WPF
一般 我们在使用TabControl时,需要添加多个tab页,然后把不需要的tab页通过鼠标右键点击ContextMenu菜单的形式进行关闭,下面的代码是直接在tab页上面添加按钮事件,直接点击关闭按 ...
- 【python之路44】tornado的用法 (二)
参考:https://www.cnblogs.com/sunshuhai/articles/6253815.html 一.代码目录构建 代码目录设置如下图: #!/usr/bin/env python ...
- 2019-2019-2-20175332-实验三《敏捷开发与XP实践》实验报告
一.编码标准 题目要求: 在IDEA中使用工具(Code->Reformate Code)把下面代码重新格式化,再研究一下Code菜单,找出一项让自己感觉最好用的功能. 实验步骤 1.安装ali ...
- 转I/O多路复用之select
源地址:http://my.oschina.net/pathenon/blog/64090 select的功能可以用一句话来描述: 实现基于I/O多路复用的异步并发编程. 在具体讲解select之前我 ...
- csdn vip文章:使用matlab模拟镜头失真
原文地址 https://blog.csdn.net/lircsszz/article/details/80249017 最近在研究图像校正,现将镜头失真中常见的径向畸变(radial distort ...
- 深喉起底APP线下预装市场,如何一夜间拥有千万用户
注:预装对于中国的移动互联网创业者有多重要?i黑马知道这样一个内幕,某商务告诉我他们公司的前2000万用户就是靠预装打下来的,总部在北京,直接派驻商务长期扎根在深圳搞定手机厂商.而这家公司初期发展得益 ...
- 【DM8168学习笔记1】帮您快速入门 TI 的 Codec Engine
http://www.ti.com.cn/general/cn/docs/gencontent.tsp?contentId=61575 德州仪器半导体技术(上海)有限公司 通用DSP 技术应用工程师 ...
- 开方运算的DSP实现
//=============================================== //函数名:VSqrt3 //功能: 实现对32位定点数的开方 //性能: 60M主频28015 ...