LeetCode072 Edit Distance
来源:互联网 发布:天下手游激活码淘宝 编辑:程序博客网 时间:2024/06/07 17:29
详细见:leetcode.com/problems/edit-distance
Java Solution: github
package leetcode;/** * 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 *//** * @author zxwtry * @email zxwtry@qq.com * @project OJ * @package leetcode * @file P072_EditDistance.java * @type P072_EditDistance * @date 2017年3月7日 下午2:45:17 * @details Solution1: AC 16ms 35.42% * @details Solution2: AC 12ms 77.30% * @details Solution3: AC 17ms 24.20% */public class P072_EditDistance {static class Solution1 { public int minDistance(String word1, String word2) { if (word1 == null || word2 == null) return minDistance(word1 == null ? "" : word1, word2 == null ? "" : word2); if (word1.length() < word2.length()) return minDistance(word2, word1); int[][] dp = new int[2][word2.length() + 1]; for (int j = 0; j <= word2.length(); j ++) dp[0][j] = j; for (int i = 1; i <= word1.length(); i ++) { dp[1][0] = i; for (int j = 1; j <= word2.length(); j ++) { dp[1][j] = Math.min(Math.min(dp[0][j] + 1, dp[1][j - 1] + 1), dp[0][j - 1] + (word1.charAt(i - 1) == word2.charAt(j - 1) ? 0 : 1)); } System.arraycopy(dp[1], 0, dp[0], 0, dp[0].length); } return dp[1][word2.length()]; }}static class Solution2 {public int minDistance(String word1, String word2) {if (word1 == null)return minDistance("", word2);if (word2 == null)return minDistance("", word1);int len1 = word1.length(), len2 = word2.length();if (len1 == 0 || len2 == 0)return len1 + len2;int[][] m = new int[len1 + 1][len2 + 1];for (int i = 0; i <= len1; i ++)m[i][0] = i;for (int j = 1; j <= len2; j ++)m[0][j] = j;for (int i = 1; i <= len1; i ++)for (int j = 1; j <= len2; j ++) {m[i][j] = Math.min(Math.min(m[i - 1][j] + 1, m[i][j - 1] + 1), m[i - 1][j - 1] + (word1.charAt(i - 1) == word2.charAt(j - 1) ? 0 : 1));}return m[len1][len2];}}static class Solution3 { public int minDistance(String word1, String word2) { if (word1 == null || word2 == null) return minDistance(word1 == null ? "" : word1, word2 == null ? "" : word2); if (word1.length() < word2.length()) return minDistance(word2, word1); int[][] dp = new int[2][word2.length() + 1]; for (int j = 0; j <= word2.length(); j ++) dp[0][j] = j; for (int i = 1; i <= word1.length(); i ++) { dp[1][0] = i; for (int j = 1; j <= word2.length(); j ++) { dp[1][j] = Math.min(Math.min(dp[0][j] + 1, dp[1][j - 1] + 1), dp[0][j - 1] + (word1.charAt(i - 1) == word2.charAt(j - 1) ? 0 : 1)); } int[] tmp = dp[0]; dp[0] = dp[1]; dp[1] = tmp; } return dp[0][word2.length()]; } }}
C Solution: github
/* url: leetcode.com/problems/edit-distance minDistance: AC 9ms 23.81% minDistance2: AC 9ms 23.81%*/#include <stdio.h>#include <stdlib.h>#include <string.h>int _min(int a, int b, int c) { if (a < b) return a < c ? a : c; return b < c ? b : c;}int minDistance(char* w1, char* w2) { int n1 = strlen(w1), n2 = strlen(w2), i = 0, j = 0; int ** m = (int**) malloc(sizeof(int*) * (n1 + 1)); for (i = 0; i <= n1; i ++) m[i] = (int*) malloc(sizeof(int) * (n2 + 1)); for (j = 0; j <= n2; j ++) m[0][j] = j; for (i = 0; i <= n1; i ++) m[i][0] = i; for (i = 0; i < n1; i ++) { for (j = 0; j < n2; j ++) { m[i+1][j+1] = _min(m[i][j]+(w1[i] == w2[j] ? 0 : 1), m[i+1][j] + 1, m[i][j+1] + 1); } } j = m[n1][n2]; for (i = 0; i <= n1; i ++) free(m[i]); free(m); return j;}void swap(int** m1, int** m2) { int* t = *m1; *m1 = *m2; *m2 = t;}int minDistance2(char* w1, char* w2) { int n1 = strlen(w1), n2 = strlen(w2), i = 0, j = 0; int *m1 = (int*) malloc(sizeof(int) * (n2 + 1)); int *m2 = (int*) malloc(sizeof(int) * (n2 + 1)); for (j = 0; j <= n2; j ++) m1[j] = j; for (i = 0; i < n1; i ++) { m2[0] = i+1; for (j = 0; j < n2; j ++) { m2[j+1] = _min(m1[j]+(w1[i] == w2[j] ? 0 : 1), m2[j] + 1, m1[j+1] + 1); } swap(&m1, &m2); } j = m1[n2]; free(m1); free(m2); return j;}int main() { char* w1 = "b"; char* w2 = ""; printf("answer is %d\r\n", minDistance(w1, w2)); return 0;}
Python Solution: github
#coding=utf-8''' url: leetcode.com/problems/edit-distance/ @author: zxwtry @email: zxwtry@qq.com @date: 2017年4月16日 @details: Solution: 342ms 17.65% @details: Solution: 199ms 94.33%'''class Solution(object): def minDistance(self, w1, w2): """ :type w1: str :type w2: str :rtype: int """ n1 = 0 if w1 == None else len(w1) n2 = 0 if w2 == None else len(w2) dp = [[0 for j in range(n2+1)] for i in range(n1+1)] for j in range(n2+1): dp[0][j] = j for i in range(n1+1): dp[i][0] = i for i in range(n1): for j in range(n2): dp[i+1][j+1] = min(dp[i][j]+(0 if w1[i] == w2[j] else 1), dp[i+1][j]+1, dp[i][j+1]+1) return dp[n1][n2]class Solution2(object): def minDistance(self, w1, w2): """ :type w1: str :type w2: str :rtype: int """ n1 = 0 if w1 == None else len(w1) n2 = 0 if w2 == None else len(w2) if n1 == 0 or n2 == 0: return n1+n2 d1, d2 = [0]*(n2+1), [0]*(n2+1) for j in range(n2+1): d1[j] = j for i in range(n1): d2[0] = i+1 for j in range(n2): d2[j+1] = min(d1[j]+(0 if w1[i] == w2[j] else 1), d2[j]+1, d1[j+1]+1) t = d1 d1 = d2 d2 = t return d1[n2] if __name__ == "__main__": w1 = "ab" w2 = "bc" print(Solution().minDistance(w1, w2))
0 0
- LeetCode072 Edit Distance
- edit distance
- Edit Distance
- edit distance
- Edit Distance
- Edit Distance
- Edit Distance
- Edit Distance
- Edit Distance
- Edit Distance
- Edit Distance
- Edit Distance
- Edit Distance
- Edit Distance
- Edit Distance
- Edit Distance
- Edit Distance
- Edit Distance
- LeetCode071 Simplify Path
- 学习MVC之租房网站(四)-实现Service层并进行单元测试
- LeetCode Week 3
- 快速排序
- 一个通用自动处理 js版本号和css 版本号、自动压缩 js,css、自动清理网页注释工具
- LeetCode072 Edit Distance
- 题目1084:整数拆分
- oracle学习笔记
- Spring AOP Annotaion
- 让我来谈谈如何写paper!
- c++知识复习
- 长尾理论
- zoj3953 贪心 training 3
- Boost---在Windows上的入门(1)