Merge pull request #1529 from cezarbbb/Backtracking03

添加 0078.子集 Rust版本
This commit is contained in:
程序员Carl
2022-08-04 08:15:32 +08:00
committed by GitHub

View File

@ -292,6 +292,30 @@ function subsets(nums: number[]): number[][] {
};
```
## Rust
```Rust
impl Solution {
fn backtracking(result: &mut Vec<Vec<i32>>, path: &mut Vec<i32>, nums: &Vec<i32>, start_index: usize) {
result.push(path.clone());
let len = nums.len();
// if start_index >= len { return; }
for i in start_index..len {
path.push(nums[i]);
Self::backtracking(result, path, nums, i + 1);
path.pop();
}
}
pub fn subsets(nums: Vec<i32>) -> Vec<Vec<i32>> {
let mut result: Vec<Vec<i32>> = Vec::new();
let mut path: Vec<i32> = Vec::new();
Self::backtracking(&mut result, &mut path, &nums, 0);
result
}
}
```
## C
```c