mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-09 11:34:46 +08:00
Merge pull request #1534 from cezarbbb/Backtracking05
添加 0491.递增子序列 Rust版本
This commit is contained in:
@ -423,6 +423,57 @@ function findSubsequences(nums: number[]): number[][] {
|
||||
};
|
||||
```
|
||||
|
||||
### Rust
|
||||
**回溯+哈希**
|
||||
```Rust
|
||||
use std::collections::HashSet;
|
||||
impl Solution {
|
||||
fn backtracking(result: &mut Vec<Vec<i32>>, path: &mut Vec<i32>, nums: &Vec<i32>, start_index: usize) {
|
||||
if path.len() > 1 { result.push(path.clone()); }
|
||||
let len = nums.len();
|
||||
let mut uset: HashSet<i32> = HashSet::new();
|
||||
for i in start_index..len {
|
||||
if (!path.is_empty() && nums[i] < *path.last().unwrap()) || uset.contains(&nums[i]) { continue; }
|
||||
uset.insert(nums[i]);
|
||||
path.push(nums[i]);
|
||||
Self::backtracking(result, path, nums, i + 1);
|
||||
path.pop();
|
||||
}
|
||||
}
|
||||
|
||||
pub fn find_subsequences(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
|
||||
}
|
||||
}
|
||||
```
|
||||
**回溯+数组**
|
||||
```Rust
|
||||
impl Solution {
|
||||
fn backtracking(result: &mut Vec<Vec<i32>>, path: &mut Vec<i32>, nums: &Vec<i32>, start_index: usize) {
|
||||
if path.len() > 1 { result.push(path.clone()); }
|
||||
let len = nums.len();
|
||||
let mut used = [0; 201];
|
||||
for i in start_index..len {
|
||||
if (!path.is_empty() && nums[i] < *path.last().unwrap()) || used[(nums[i] + 100) as usize] == 1 { continue; }
|
||||
used[(nums[i] + 100) as usize] = 1;
|
||||
path.push(nums[i]);
|
||||
Self::backtracking(result, path, nums, i + 1);
|
||||
path.pop();
|
||||
}
|
||||
}
|
||||
|
||||
pub fn find_subsequences(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