Leet Code 72 Edit Distance - 编辑距离 - Java
2016-06-25 00:00
281 查看
摘要: Leet Code 72 Edit Distance - 编辑距离 - Java
问题原始链接 https://leetcode.com/problems/edit-distance
给定两个单词 word1 和 word2,找到把 word1 转变成 word2 的最小步数。(每次操作记为一步)
允许对一个单词进行以下三种操作:
插入一个字符
删除一个字符
替换一个字符
思路:使用动态规划法。申请一个整型数组 dp[word1.length+1][word2.length+1]。
dp[i+1][j+1] 表示把 word1[0..i] 变成 word2[0..j] 需要的最小步数。
dp[0][j+1] 表示把空串变成 word2[0..j]需要的最小步数,很明显需要插入 j+1 个字符,所以 dp[0][j+1]=j+1。
dp[i+1][0] 表示把 word1[0..i] 变成空串需要的最小步数,很明显需要删除 i+1 个字符,所以 dp[i+1][0]=i+1。
如果 word1[i] == word2[j],只需要把 word1[0..i-1] 变成 word2[0..j-1],所以 dp[i+1][j+1]=dp[i][j]。
如果 word1[i] != word2[j],把 word1[0..i] 变成 word2[0..j] 有三种方式:
先把 word1[0..i-1] 变成 word2[0..j],然后删除 word1[i]
先把 word1[0..i] 变成 word2[0..j-1],然后插入 word2[j]
先把 word1[0..i-1] 变成 word2[0..j-1],然后把 word1[i] 替换成 word2[j]
取这三种方式中的最小值,dp[i+1][j+1]=min(dp[i][j+1],dp[i+1][j],dp[i][j])+1。
最后 dp[word1.length][word2.length] 即为最终结果。
问题原始链接 https://leetcode.com/problems/edit-distance
给定两个单词 word1 和 word2,找到把 word1 转变成 word2 的最小步数。(每次操作记为一步)
允许对一个单词进行以下三种操作:
插入一个字符
删除一个字符
替换一个字符
思路:使用动态规划法。申请一个整型数组 dp[word1.length+1][word2.length+1]。
dp[i+1][j+1] 表示把 word1[0..i] 变成 word2[0..j] 需要的最小步数。
dp[0][j+1] 表示把空串变成 word2[0..j]需要的最小步数,很明显需要插入 j+1 个字符,所以 dp[0][j+1]=j+1。
dp[i+1][0] 表示把 word1[0..i] 变成空串需要的最小步数,很明显需要删除 i+1 个字符,所以 dp[i+1][0]=i+1。
如果 word1[i] == word2[j],只需要把 word1[0..i-1] 变成 word2[0..j-1],所以 dp[i+1][j+1]=dp[i][j]。
如果 word1[i] != word2[j],把 word1[0..i] 变成 word2[0..j] 有三种方式:
先把 word1[0..i-1] 变成 word2[0..j],然后删除 word1[i]
先把 word1[0..i] 变成 word2[0..j-1],然后插入 word2[j]
先把 word1[0..i-1] 变成 word2[0..j-1],然后把 word1[i] 替换成 word2[j]
取这三种方式中的最小值,dp[i+1][j+1]=min(dp[i][j+1],dp[i+1][j],dp[i][j])+1。
最后 dp[word1.length][word2.length] 即为最终结果。
[code=language-java]public class Solution { public static int minDistance(String word1, String word2) { int[][] dp = new int[word1.length() + 1][word2.length() + 1]; for (int j = 1; j <= word2.length(); j++) { dp[0][j] = j; } for (int i = 1; i <= word1.length(); i++) { dp[i][0] = i; } for (int i = 0; i < word1.length(); i++) { for (int j = 0; j < word2.length(); j++) { if (word1.charAt(i) == word2.charAt(j)) { dp[i + 1][j + 1] = dp[i][j]; } else { dp[i + 1][j + 1] = Math.min(Math.min(dp[i + 1][j], dp[i][j + 1]), dp[i][j]) + 1; } } } return dp[word1.length()][word2.length()]; } }
相关文章推荐
- leetcode 179 Largest Number
- leetcode 24 Swap Nodes in Pairs
- leetcode 2 Add Two Numbers 方法1
- leetcode 2 Add Two Numbers 方法2
- leetcode----Longest Substring Without Repeating Characters
- [LeetCode]47 Permutations II
- [LeetCode]65 Valid Number
- [LeetCode]123 Best Time to Buy and Sell Stock III
- [LeetCode] String Reorder Distance Apart
- [LeetCode] Sliding Window Maximum
- [LeetCode] Find the k-th Smallest Element in the Union of Two Sorted Arrays
- [LeetCode] Determine If Two Rectangles Overlap
- [LeetCode] A Distance Maximizing Problem
- leetcode_linearList
- leetcode_linearList02
- 021-Merge Two Sorted Lists(合并两个排好序的单链表);leetcode
- LeetCode[Day 1] Two Sum 题解
- LeetCode[Day 2] Median of Two Sorted Arrays 题解
- LeetCode[Day 3] Longest Substring Without... 题解
- LeetCode [Day 4] Add Two Numbers 题解