Skip to content

Commit 7f69bc6

Browse files
authored
feat: add rust solution to lc problem: No.0022 (doocs#1252)
1 parent 07557bb commit 7f69bc6

File tree

2 files changed

+62
-0
lines changed

2 files changed

+62
-0
lines changed

solution/0000-0099/0022.Generate Parentheses/README.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,37 @@ impl Solution {
219219
}
220220
```
221221

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+
222253
### **...**
223254

224255
```

solution/0000-0099/0022.Generate Parentheses/README_EN.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,37 @@ impl Solution {
191191
}
192192
```
193193

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+
194225
### **...**
195226

196227
```

0 commit comments

Comments
 (0)