mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-08 16:54:50 +08:00
添加 0046.全排列 Rust版本
添加 0046.全排列 Rust版本
This commit is contained in:
@ -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
|
||||
|
Reference in New Issue
Block a user