1
+ // 思路一 自顶向下的备忘录动态规划
2
+ // 如果不使用备忘录就会超时
3
+ // 16ms 8.41%
4
+
5
+ #include < string>
6
+ using namespace std ;
7
+
8
+ class Solution {
9
+ public:
10
+ int minDistance (string word1, string word2)
11
+ {
12
+ // 申请空间,这里注意一下二维动态数组的申请
13
+ int **steps = new int *[word1.length () + 1 ];
14
+ for (int i = 0 ; i <= word1.length (); i++)
15
+ steps[i] = new int [word2.length () + 1 ];
16
+
17
+ // 初始化
18
+ for (int i = 0 ; i <= word1.length (); i++)
19
+ for (int j = 0 ; j <= word2.length (); j++)
20
+ steps[i][j] = -1 ;
21
+
22
+ // 动态规划
23
+ int res = dp (word1, word2, steps);
24
+
25
+ // 释放空间
26
+ delete[] steps;
27
+
28
+ return res;
29
+ }
30
+
31
+ int dp (string word1, string word2, int **steps)
32
+ {
33
+ if (steps[word1.length ()][word2.length ()] == - 1 )
34
+ {
35
+ // 判断两个字符串是否为空
36
+ if (word1 == " " && word2 == " " )
37
+ steps[word1.length ()][word2.length ()] = 0 ;
38
+ else if (word1 == " " )
39
+ steps[word1.length ()][word2.length ()] = word2.length ();
40
+ else if (word2 == " " )
41
+ steps[word1.length ()][word2.length ()] = word1.length ();
42
+ else
43
+ {
44
+ // 如果两者都不为空的情况下进行递归的判断
45
+ // 如果两个字符串的最后一位是相等的
46
+ if (word2[word2.length () - 1 ] == word1[word1.length () - 1 ])
47
+ steps[word1.length ()][word2.length ()] = dp (word1.substr (0 , word1.length () - 1 ), word2.substr (0 , word2.length () - 1 ), steps);
48
+
49
+ // 不相等
50
+ else
51
+ {
52
+ // 删除最后一个
53
+ int stepDelete = dp (word1.substr (0 , word1.length () - 1 ), word2.substr (0 , word2.length ()), steps);
54
+
55
+ // 替换最后一个
56
+ int stepReplace = dp (word1.substr (0 , word1.length () - 1 ), word2.substr (0 , word2.length () - 1 ), steps);
57
+
58
+ // 在最后一位上插入
59
+ int stepInsert = dp (word1.substr (0 , word1.length ()), word2.substr (0 , word2.length () - 1 ), steps);
60
+
61
+ steps[word1.length ()][word2.length ()] = minThree (stepDelete, stepReplace, stepInsert) + 1 ;
62
+ }
63
+ }
64
+ }
65
+ return steps[word1.length ()][word2.length ()];
66
+ }
67
+
68
+ inline int minThree (int a, int b, int c)
69
+ {
70
+ int temp = a < b ? a : b;
71
+ return temp < c ? temp : c;
72
+ }
73
+ };
74
+
75
+
76
+ // 思路二 自底向上的递推动规 节省空间版本
77
+ // 4ms 100%
78
+
79
+ class Solution {
80
+ public:
81
+ int minDistance (string word1, string word2)
82
+ {
83
+ // 初始化第一行的结果
84
+ int *temp = new int [word2.length () + 1 ];
85
+ int *steps = new int [word2.length () + 1 ];
86
+ for (int i = 0 ; i <= word2.length (); i++)
87
+ temp[i] = i;
88
+
89
+ for (int i = 1 ; i <= word1.length (); i++)
90
+ {
91
+ steps[0 ] = i;
92
+ for (int j = 1 ; j <= word2.length (); j++)
93
+ {
94
+ if (word1[i - 1 ] == word2[j - 1 ])
95
+ steps[j] = temp[j - 1 ];
96
+ else
97
+ {
98
+ int stepDelete = temp[j];
99
+ int stepReplace = temp[j - 1 ];
100
+ int stepInsert = steps[j - 1 ];
101
+
102
+ int temp = stepDelete < stepReplace ? stepDelete : stepReplace;
103
+ steps[j] = 1 + (temp < stepInsert ? temp : stepInsert);
104
+ }
105
+ }
106
+
107
+ for (int j = 0 ; j <= word2.length (); j++)
108
+ temp[j] = steps[j];
109
+ }
110
+
111
+ int res = temp[wpord2.length ()];
112
+
113
+ // 释放空间
114
+ delete[] steps;
115
+ delete[] temp;
116
+
117
+ return res;
118
+ }
119
+ };
0 commit comments