mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-08 00:43:04 +08:00
Merge pull request #1529 from cezarbbb/Backtracking03
添加 0078.子集 Rust版本
This commit is contained in:
@ -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
|
||||
|
Reference in New Issue
Block a user