leetcode — edit-distance
/**
* Source : https://oj.leetcode.com/problems/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:
*
* a) Insert a character
* b) Delete a character
* c) Replace a character
*/
public class EditDistance {
/**
* 计算出从一个单词变到另一个单词的最少步数,也就是最短距离,只能使用插入、删除、替换操作
*
* 考虑两个单词abc,bbcd,dp[i][j]表示word1的前i个字符变到word2的前j个字符,所需要的步数,""表示空串
* "" a b c
* "" 0 1 2 3
* b 1 1 1 2
* b 1 1 1 2
* c 3 3 2 1
* d 4 4 3 2
*
* 从上面的演算可以看出
* 当word1[i] == word2[j]的时候,dp[i][j] = dp[i-1][j-1]
* 当word1[i] != word2[j]的时候,dp[i][j] = 其左边、左上方、正上方三个数字中最小的那一个加1
*
*
* @param word1
* @param word2
* @return
*/
public int minimumDistance (String word1, String word2) {
if (word1.length() == 0) {
return word2.length();
}
if (word2.length() == 0) {
return word1.length();
}
int[][] dp = new int[word1.length() + 1][word2.length() + 1];
for (int i = 0; i <= word1.length(); i++) {
dp[i][0] = i;
}
for (int i = 0; i <= word2.length(); i++) {
dp[0][i] = i;
}
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(Math.min(dp[i-1][j], dp[i][j-1]), dp[i-1][j-1]) + 1;
}
}
}
return dp[word1.length()][word2.length()];
}
public static void main(String[] args) {
EditDistance editDistance = new EditDistance();
System.out.println(editDistance.minimumDistance("", "abc"));
System.out.println(editDistance.minimumDistance("b", "abc"));
System.out.println(editDistance.minimumDistance("bb", "abc"));
System.out.println(editDistance.minimumDistance("bbc", "abc"));
System.out.println(editDistance.minimumDistance("bbcd", "abc"));
}
}
leetcode — edit-distance的更多相关文章
- [LeetCode] Edit Distance 编辑距离
Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2 ...
- Leetcode:Edit Distance 解题报告
Edit Distance Given two words word1 and word2, find the minimum number of steps required to convert ...
- [leetcode]Edit Distance @ Python
原题地址:https://oj.leetcode.com/problems/edit-distance/ 题意: Given two words word1 and word2, find the m ...
- [LeetCode] Edit Distance 字符串变换为另一字符串动态规划
Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2 ...
- Leetcode Edit Distance
Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2 ...
- [LeetCode] Edit Distance(很好的DP)
Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2 ...
- LeetCode: Edit Distance && 子序列题集
Title: Given two words word1 and word2, find the minimum number of steps required to convert word1 t ...
- LeetCode——Edit Distance
Question Given two words word1 and word2, find the minimum number of steps required to convert word1 ...
- [LeetCode] One Edit Distance 一个编辑距离
Given two strings S and T, determine if they are both one edit distance apart. 这道题是之前那道Edit Distance ...
- 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 ...
随机推荐
- C++二分图匹配基础:zoj1002 FireNet 火力网
直接给出题目吧... 问题 D(1988): [高级算法]火力网 时间限制: 1 Sec 内存限制: 128 MB 题目描述 给出一个N*N的网格,用'.'表示空地,用'X'表示墙.在网格上放碉堡,可 ...
- Python——我所学习的turtle函数库
1基础概念 1.1 画布(canvas) 画布就是turtle为我们展开用于绘图区域, 我们可以设置它的大小和初始位置. 常用的画布方法有两个:screensize()和setup(). (1)tur ...
- 读《赋能》有感zz
听樊登18年春节后第一本新书<赋能>,学到了几个新的管理词语,深井病.还原论.乌卡时代: 下面谈谈自己的学习收获. 深井病就是随着组织发展的壮大,当然是传统的企业,其部门或个人都会变得越来 ...
- 2019.03.11 bzoj4813: [Cqoi2017]小Q的棋盘(贪心)
传送门 考虑最后所有走过的点构成的树,显然除了最长链走一遍以外每条轻链都走两遍. 于是求一波最长链搞一搞就完了. 注意几个小细节特判qwq 代码: #include<bits/stdc++.h& ...
- oracle控制台命令
sqlplus /nolog -- 无密码登陆 sqlplus "/as sysdba" -- 以管理员身份登录 shutdown immediate; --关闭数据库 sel ...
- ps最最基础的文档
因为学习PHP,但是公司没有前端工程师,修图的时候只好找被人帮忙,一个简答的问题,其实几分钟就搞定了,还要麻烦别人,就自己学了一下ps.一共花了3天时间.学习了一些简单的操作. 工具:Adobe Ph ...
- 【原创】IO流:读写操作研究(输入流)
默写代码(以下问题要求能默写,不翻书不百度) 输入 问题一:从文件abc.txt中读取数据到字节数组并打印出来. 分析:如果读取数据,首先第一个问题,数据有多少?如果数据量不确定,如果确定字节数组大小 ...
- 20145232韩文浩《网络对抗》MSF基础应用
MS08-067漏洞攻击 攻击机:Kali:192.168.31.132 靶机:win XP SP3(English):192.168.31.180 在VMware中设置两台虚拟机网络为NAT模式,自 ...
- STM32CubeMX的串口配置,以及驱动代码
1.STM32CubeMX的配置没啥子好说的,使能然后改一下波特率和字长,然后在将中断勾选,把中断等级调到1(一定要比systick的优先级垃圾!!!) 2.驱动代码 在生成的it.c文件中,例如用的 ...
- Spring注解使用注意点
1 @RestController @Controller @RestController注解相当于@ResponseBody + @Controller合在一起的作用. 如果只是使用@RestC ...