Merge pull request #1535 from cezarbbb/Backtracking06

添加 0046.全排列 Rust版本
This commit is contained in:
程序员Carl
2022-07-22 09:52:19 +08:00
committed by GitHub

View File

@ -359,6 +359,36 @@ function permute(nums: number[]): number[][] {
}; };
``` ```
### Rust
```Rust
impl Solution {
fn backtracking(result: &mut Vec<Vec<i32>>, path: &mut Vec<i32>, nums: &Vec<i32>, used: &mut Vec<bool>) {
let len = nums.len();
if path.len() == len {
result.push(path.clone());
return;
}
for i in 0..len {
if used[i] == true { continue; }
used[i] = true;
path.push(nums[i]);
Self::backtracking(result, path, nums, used);
path.pop();
used[i] = false;
}
}
pub fn permute(nums: Vec<i32>) -> Vec<Vec<i32>> {
let mut result: Vec<Vec<i32>> = Vec::new();
let mut path: Vec<i32> = Vec::new();
let mut used = vec![false; nums.len()];
Self::backtracking(&mut result, &mut path, &nums, &mut used);
result
}
}
```
### C ### C
```c ```c