@@ -90,36 +90,39 @@ class Solution {
90
90
}
91
91
}
92
92
```
93
- TypeScript 代码:
94
- ``` TypeScript
95
- function shortestCommonSupersequence(s1 : string , s2 : string ): string {
96
- const n = s1 .length , m = s2 .length
97
- s1 = " " + s1 ; s2 = " " + s2
98
- const f = new Array <Array <number >>()
99
- for (let i = 0 ; i < n + 10 ; i ++ ) f .push (new Array <number >(m + 10 ).fill (0 ))
100
- for (let i = 1 ; i <= n ; i ++ ) {
101
- for (let j = 1 ; j <= m ; j ++ ) {
102
- if (s1 [i ] == s2 [j ]) f [i ][j ] = f [i - 1 ][j - 1 ] + 1
103
- else f [i ][j ] = Math .max (f [i - 1 ][j ], f [i ][j - 1 ])
93
+ C++ 代码:
94
+ ``` C++
95
+ class Solution {
96
+ public:
97
+ string shortestCommonSupersequence(string str1, string str2) {
98
+ int n = str1.size(), m = str2.size();
99
+ str1 = " " + str1; str2 = " " + str2;
100
+ vector<vector<int >> f(n + 10, vector<int >(m + 10));
101
+ for (int i = 1; i <= n; ++i) {
102
+ for (int j = 1; j <= m; ++j) {
103
+ if (str1[ i] == str2[ j] )
104
+ f[ i] [ j ] = f[ i - 1] [ j - 1 ] + 1;
105
+ else
106
+ f[ i] [ j ] = max(f[ i - 1] [ j ] , f[ i] [ j - 1 ] );
107
+ }
104
108
}
105
- }
106
- let ans = " "
107
- let i = n , j = m
108
- while (i > 0 || j > 0 ) {
109
- if (i == 0 ) ans += s2 [j -- ]
110
- else if (j == 0 ) ans += s1 [i -- ]
111
- else {
112
- if (s1 [i ] == s2 [j ]) {
113
- ans += s1 [i ]
114
- i -- ; j --
115
- } else if (f [i ][j ] == f [i - 1 ][j ]) {
116
- ans += s1 [i -- ]
117
- } else {
118
- ans += s2 [j -- ]
109
+ string res;
110
+ int i = n, j = m;
111
+ while (i > 0 || j > 0) {
112
+ if (i == 0) res += str2[ j--] ;
113
+ else if (j == 0) res += str1[ i--] ;
114
+ else {
115
+ if (str1[ i] == str2[ j] ) {
116
+ res += str1[ i] ;
117
+ i--; j--;
118
+ }
119
+ else if (f[ i] [ j ] == f[ i - 1] [ j ] ) res += str1[ i--] ;
120
+ else res += str2[ j--] ;
119
121
}
120
122
}
123
+ reverse(res.begin(), res.end());
124
+ return res;
121
125
}
122
- return ans .split (' ' ).reverse ().join (' ' )
123
126
};
124
127
```
125
128
Python 代码:
@@ -155,6 +158,38 @@ class Solution:
155
158
j -= 1
156
159
return ans[::-1]
157
160
```
161
+ TypeScript 代码:
162
+ ``` TypeScript
163
+ function shortestCommonSupersequence(s1 : string , s2 : string ): string {
164
+ const n = s1 .length , m = s2 .length
165
+ s1 = " " + s1 ; s2 = " " + s2
166
+ const f = new Array <Array <number >>()
167
+ for (let i = 0 ; i < n + 10 ; i ++ ) f .push (new Array <number >(m + 10 ).fill (0 ))
168
+ for (let i = 1 ; i <= n ; i ++ ) {
169
+ for (let j = 1 ; j <= m ; j ++ ) {
170
+ if (s1 [i ] == s2 [j ]) f [i ][j ] = f [i - 1 ][j - 1 ] + 1
171
+ else f [i ][j ] = Math .max (f [i - 1 ][j ], f [i ][j - 1 ])
172
+ }
173
+ }
174
+ let ans = " "
175
+ let i = n , j = m
176
+ while (i > 0 || j > 0 ) {
177
+ if (i == 0 ) ans += s2 [j -- ]
178
+ else if (j == 0 ) ans += s1 [i -- ]
179
+ else {
180
+ if (s1 [i ] == s2 [j ]) {
181
+ ans += s1 [i ]
182
+ i -- ; j --
183
+ } else if (f [i ][j ] == f [i - 1 ][j ]) {
184
+ ans += s1 [i -- ]
185
+ } else {
186
+ ans += s2 [j -- ]
187
+ }
188
+ }
189
+ }
190
+ return ans .split (' ' ).reverse ().join (' ' )
191
+ };
192
+ ```
158
193
* 时间复杂度:` LCS ` 复杂度为 $O(n \times m)$;构造答案复杂度为 $O(n \times m)$。整体复杂度为 $O(n \times m)$
159
194
* 空间复杂度:$O(n \times m)$
160
195
0 commit comments