-
SLPHPython解法一:最长公共子序列
class Solution(object): def minDistance(self, word1, word2): """ :type word1: str :type word2: str :rtype: int """ return len(word1) + len(word2) - 2 * self.lcs(word1, word2) def lcs(self, word1, word2): len1, len2 = len(word1), len(word2) dp = [[0] * (len2 + 1) for x in range(len1 + 1)] for x in range(len1): for y in range(len2): dp[x + 1][y + 1] = max(dp[x][y + 1], dp[x + 1][y]) if word1[x] == word2[y]: dp[x + 1][y + 1] = dp[x][y] + 1 return dp[len1][len2]
-
SLPHPython解法二:动态规划
class Solution(object): def minDistance(self, word1, word2): """ :type word1: str :type word2: str :rtype: int """ len1, len2 = len(word1), len(word2) dp = [[0] * (len2 + 1) for x in range(len1 + 1)] for x in range(len1 + 1): for y in range(len2 + 1): if x == 0 or y == 0: dp[x][y] = x + y elif word1[x - 1] == word2[y - 1]: dp[x][y] = dp[x - 1][y - 1] else: dp[x][y] = min(dp[x - 1][y], dp[x][y - 1]) + 1 return dp[len1][len2]
-
SLPHC++解法一:
class Solution { public: int minDistance(string word1, string word2) { int n1 = word1.size(), n2 = word2.size(); vector<vector<int>> dp(n1 + 1, vector<int>(n2 + 1, 0)); for (int i = 1; i <= n1; ++i) { for (int j = 1; j <= n2; ++j) { if (word1[i - 1] == word2[j - 1]) { dp[i][j] = dp[i - 1][j - 1] + 1; } else { dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]); } } } return n1 + n2 - 2 * dp[n1][n2]; } };
-
SLPHC++解法二:
class Solution { public: int minDistance(string word1, string word2) { int n1 = word1.size(), n2 = word2.size(); vector<vector<int>> dp(n1 + 1, vector<int>(n2 + 1, 0)); for (int i = 0; i <= n1; ++i) dp[i][0] = i; for (int j = 0; j <= n2; ++j) dp[0][j] = j; for (int i = 1; i <= n1; ++i) { for (int j = 1; j <= n2; ++j) { if (word1[i - 1] == word2[j - 1]) { dp[i][j] = dp[i - 1][j - 1]; } else { dp[i][j] = 1 + min(dp[i - 1][j], dp[i][j - 1]); } } } return dp[n1][n2]; } };
-
SLPHC++解法三:
class Solution { public: int minDistance(string word1, string word2) { int n1 = word1.size(), n2 = word2.size(); vector<vector<int>> memo(n1 + 1, vector<int>(n2 + 1, 0)); return helper(word1, word2, 0, 0, memo); } int helper(string word1, string word2, int p1, int p2, vector<vector<int>>& memo) { if (memo[p1][p2] != 0) return memo[p1][p2]; int n1 = word1.size(), n2 = word2.size(); if (p1 == n1 || p2 == n2) return n1 - p1 + n2 - p2; if (word1[p1] == word2[p2]) { memo[p1][p2] = helper(word1, word2, p1 + 1, p2 + 1, memo); } else { memo[p1][p2] = 1 + min(helper(word1, word2, p1 + 1, p2, memo), helper(word1, word2, p1, p2 + 1, memo)); } return memo[p1][p2]; } };
-