题目描述

地址 : https://leetcode.com/problems/edit-distance/description/

思路

  • 使用dp[i][j]用来表示word10~i-1word20~j-1的最小编辑距离
  • 我们可以知道边界情况:dp[i][0] = idp[0][j] = j,代表从 "" 变为 dp[0~i-1]dp[0][0~j-1] 所需要的次数

同时对于两个字符串的子串,都能分为最后一个字符相等或者不等的情况:

  • 如果word1[i-1] == word2[j-1]dp[i][j] = dp[i-1][j-1]
  • 如果word1[i-1] != word2[j-1]
    • 向word1插入:dp[i][j] = dp[i][j-1] + 1
    • 从word1删除:dp[i][j] = dp[i-1][j] + 1
    • 替换word1元素:dp[i][j] = dp[i-1][j-1] + 1
 public int minDistance(String word1, String word2) {
int n = word1.length();
int m = word2.length();
int[][] dp = new int[n + 1][m + 1];
for (int i = 0; i < m + 1; i++) {
dp[0][i] = i;
}
for (int i = 0; i < n + 1; i++) {
dp[i][0] = i;
}
for (int i = 1; i < n + 1; i++) {
for (int j = 1; j < m + 1; 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[n][m];
}

Java解决LeetCode72题 Edit Distance的更多相关文章

  1. 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 ...

  2. [Swift]LeetCode72. 编辑距离 | Edit Distance

    Given two words word1 and word2, find the minimum number of operations required to convert word1 to  ...

  3. [UCSD白板题] Compute the Edit Distance Between Two Strings

    Problem Introduction The edit distinct between two strings is the minimum number of insertions, dele ...

  4. ✡ leetcode 161. One Edit Distance 判断两个字符串是否是一步变换 --------- java

    Given two strings S and T, determine if they are both one edit distance apart. 给定两个字符串,判断他们是否是一步变换得到 ...

  5. 动态规划小结 - 二维动态规划 - 时间复杂度 O(n*n)的棋盘型,题 [LeetCode] Minimum Path Sum,Unique Paths II,Edit Distance

    引言 二维动态规划中最常见的是棋盘型二维动态规划. 即 func(i, j) 往往只和 func(i-1, j-1), func(i-1, j) 以及 func(i, j-1) 有关 这种情况下,时间 ...

  6. 刷题72. Edit Distance

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

  7. 72. Edit Distance

    题目: Given two words word1 and word2, find the minimum number of steps required to convert word1 to w ...

  8. LeetCode One Edit Distance

    原题链接在这里:https://leetcode.com/problems/one-edit-distance/ Given two strings S and T, determine if the ...

  9. Minimum edit distance(levenshtein distance)(最小编辑距离)初探

    最小编辑距离的定义:编辑距离(Edit Distance),又称Levenshtein距离.是指两个字串之间,由一个转成还有一个所需的最少编辑操作次数.许可的编辑操作包含将一个字符替换成还有一个字符. ...

随机推荐

  1. lintcode-514-栅栏染色

    514-栅栏染色 我们有一个栅栏,它有n个柱子,现在要给柱子染色,有k种颜色可以染. 必须保证不存在超过2个相邻的柱子颜色相同,求有多少种染色方案. 注意事项 n和k都是非负整数 样例 n = 3, ...

  2. week4a:个人博客作业

    本周结对项目的要求: 黄金点游戏是一个数字小游戏,其游戏规则是: N个同学(N通常大于10),每人写一个0~100之间的有理数 (不包括0或100),交给裁判,裁判算出所有数字的平均值,然后乘以0.6 ...

  3. 自动创建web.xml

    摘自:http://blog.csdn.net/weiral/article/details/51366485 今天在学习JSP时先创建了一个web项目,后来在用到web.xml文件时,才发现项目创建 ...

  4. 关于css伪类

    p:nth-child(2){} !选择所有p元素的第二个子元素:  p:nth-of-type(2) !选择所有p元素第二个为p的子元素(是选择第二个类型为p的元素 而不是第二个子集为p的元素)

  5. mysql按日期分组统计数据

    最近在做一个招聘网时,需要显示一个月内企业招聘信息的发布数量,按日期分组统计,刚开始是直接从源数据库表里面进行group by,但这样子就出现日期不连续的问题了,我想要的效果是,若当天没有数据,则显示 ...

  6. golang build 的简单用法.(菜鸟初学)

    1. golang 里面的 go build 可以编译代码. go build helloworld.go 2. 这里面有一个注意事项事项. 如果引用非go语言的 内置package的话 需要在环境变 ...

  7. [PPT] PPT 录制视频功能.

    1. 需要PPT 里面增加进截图, 发现还不如 直接插入视频合理 本来想了一种方式是 使用 screen to gif 的工具 生成gif 来处理. 后来 发现ppt 里面自带一个 屏幕录制功能. 2 ...

  8. 移动端 H5 弹出层 fixed 内容可滚动

    <div class="alert_wapper"> <div class="wapper"> <!--内容 --> < ...

  9. Ubuntu 14.04(64bit)使用mentohust连接校园网

    ubuntu14.04系统安装成功之后,需要连接上网络才可以对更新系统以及安装一些必须包.而在学校中,经常遇到的情况需要通过锐捷客户端来连接校园网. 更新: 在Ubuntu14.04下面不用装ment ...

  10. 再看select语句

    select语句是整个sql中输出的最后一条语句,这条语句是在最后输出的结果集合上做计算, 这些计算都包括啥东西呢?对每个结果集合做插值计算,在做完group by和where子句之后,那么就是一个一 ...