File tree Expand file tree Collapse file tree 2 files changed +62
-0
lines changed
solution/0000-0099/0022.Generate Parentheses Expand file tree Collapse file tree 2 files changed +62
-0
lines changed Original file line number Diff line number Diff line change @@ -219,6 +219,37 @@ impl Solution {
219
219
}
220
220
```
221
221
222
+ ``` rust
223
+ impl Solution {
224
+ pub fn generate_parenthesis (n : i32 ) -> Vec <String > {
225
+ let mut dp : Vec <Vec <String >> = vec! [vec! []; n as usize + 1 ];
226
+
227
+ // Initialize the dp vector
228
+ dp [0 ]. push (String :: from ("" ));
229
+ dp [1 ]. push (String :: from (" ()" ));
230
+
231
+ // Begin the actual dp process
232
+ for i in 2 ..= n as usize {
233
+ for j in 0 .. i as usize {
234
+ let dp_c = dp . clone ();
235
+ let first_half = & dp_c [j ];
236
+ let second_half = & dp_c [i - j - 1 ];
237
+
238
+ for f in first_half {
239
+ for s in second_half {
240
+ let f_c = f . clone ();
241
+ let cur_str = f_c + " (" + & * s + " )" ;
242
+ dp [i ]. push (cur_str );
243
+ }
244
+ }
245
+ }
246
+ }
247
+
248
+ dp [n as usize ]. clone ()
249
+ }
250
+ }
251
+ ```
252
+
222
253
### ** ...**
223
254
224
255
```
Original file line number Diff line number Diff line change @@ -191,6 +191,37 @@ impl Solution {
191
191
}
192
192
```
193
193
194
+ ``` rust
195
+ impl Solution {
196
+ pub fn generate_parenthesis (n : i32 ) -> Vec <String > {
197
+ let mut dp : Vec <Vec <String >> = vec! [vec! []; n as usize + 1 ];
198
+
199
+ // Initialize the dp vector
200
+ dp [0 ]. push (String :: from ("" ));
201
+ dp [1 ]. push (String :: from (" ()" ));
202
+
203
+ // Begin the actual dp process
204
+ for i in 2 ..= n as usize {
205
+ for j in 0 .. i as usize {
206
+ let dp_c = dp . clone ();
207
+ let first_half = & dp_c [j ];
208
+ let second_half = & dp_c [i - j - 1 ];
209
+
210
+ for f in first_half {
211
+ for s in second_half {
212
+ let f_c = f . clone ();
213
+ let cur_str = f_c + " (" + & * s + " )" ;
214
+ dp [i ]. push (cur_str );
215
+ }
216
+ }
217
+ }
218
+ }
219
+
220
+ dp [n as usize ]. clone ()
221
+ }
222
+ }
223
+ ```
224
+
194
225
### ** ...**
195
226
196
227
```
You can’t perform that action at this time.
0 commit comments