1 class Solution {
 2 private:
 3     int d[100][100];
 4     
 5 public:
 6     int minDistance(string word1, string word2)
 7     {
 8         int len1 = word1.length();
 9         int len2 = word2.length();
10         
11         for(int i=0;i<=len1;i++)
12             d[i][0]= i;
13         
14         for(int j=0;j<=len2;j++)
15             d[0][j]=j;
16         
17         for(int i=1;i <=len1;i++)
18         {
19             for(int j=1;j<=len2;j++)
20             {
21                 int diff;
22                 if(word1[i-1] == word2[j-1])
23                     diff = 0 ;
24                 else
25                     diff = 1 ;
26                 int temp = min(d[i-1][j] + 1, d[i][j-1] + 1);
27                 d[i][j] = min(temp, d[i-1][j-1] + diff);
28             }
29         }
30         return d[len1][len2];
31     }
32 };

这是《趣学算法》一书中4-4节提供的代码,在leetcode上运行,速度比较慢,392ms(5.16%),12.9mb(5.22%)。

补充一个python的实现:

 1 class Solution:
 2     def minDistance(self, word1: str, word2: str) -> int:
 3         m = len(word1)
 4         n = len(word2)
 5         dp = [[0 for _ in range(n+1)]for _ in range(m+1)]
 6         for j in range(n+1):
 7             dp[0][j] = j
 8         for i in range(m+1):
 9             dp[i][0] = i
10         for i in range(1,m+1):
11             for j in range(1,n+1):
12                 diff = 1 if word1[i-1] != word2[j-1] else 0
13                 minval = min(dp[i-1][j]+1,dp[i][j-1]+1)
14                 minval = min(minval,dp[i-1][j-1]+diff)
15                 dp[i][j] = minval
16         return dp[m][n]

240ms,16.4mb

 

下面提供leetcode中一个12ms,8.5mb的解决方案:

 1 class Solution {
 2 public:
 3     int minDistance(string word1, string word2) {
 4         int m = word1.length();
 5         int n = word2.length();
 6         vector<int> dp(m+1, 0);
 7         int i, j;
 8         int temp, min;
 9         for(i=0;i<=m;i++)
10             dp[i] = i;
11         for(j=1;j<=n;j++)
12         {
13             temp = dp[0];
14             dp[0]++;
15             for(i=1;i<=m;i++)
16             {
17                 min = temp + (word1[i-1]!=word2[j-1]);
18                 min = (dp[i-1]+1)<min?(dp[i-1]+1):min;
19                 min = (dp[i]+1)<min?(dp[i]+1):min;
20                 temp = dp[i];
21                 dp[i] = min;
22             }
23         }
24         return dp[m];
25     }
26 };

将第二种写法,转化为python语法:

 1 class Solution:
 2     def minDistance(self, word1: str, word2: str) -> int:
 3         m = len(word1)
 4         n = len(word2)
 5         dp = list(range(m+1))
 6         for j in range(1,n+1):
 7             temp = dp[0]
 8             dp[0] += 1
 9             for i in range(1,m+1):
10                 diff = 1 if word1[i-1] != word2[j-1] else 0
11                 minval = temp + diff
12                 minval = min(minval,dp[i-1]+1)
13                 minval = min(minval,dp[i]+1)
14                 temp = dp[i]
15                 dp[i] = minval
16         return dp[m]

204ms,12.9mb

posted on 2019-03-06 08:50  Sempron2800+  阅读(252)  评论(0编辑  收藏  举报