diff --git a/problems/0491.递增子序列.md b/problems/0491.递增子序列.md index 080984f2..6a92aa21 100644 --- a/problems/0491.递增子序列.md +++ b/problems/0491.递增子序列.md @@ -423,6 +423,57 @@ function findSubsequences(nums: number[]): number[][] { }; ``` +### Rust +**回溯+哈希** +```Rust +use std::collections::HashSet; +impl Solution { + fn backtracking(result: &mut Vec>, path: &mut Vec, nums: &Vec, start_index: usize) { + if path.len() > 1 { result.push(path.clone()); } + let len = nums.len(); + let mut uset: HashSet = 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) -> Vec> { + let mut result: Vec> = Vec::new(); + let mut path: Vec = Vec::new(); + Self::backtracking(&mut result, &mut path, &nums, 0); + result + } +} +``` +**回溯+数组** +```Rust +impl Solution { + fn backtracking(result: &mut Vec>, path: &mut Vec, nums: &Vec, 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) -> Vec> { + let mut result: Vec> = Vec::new(); + let mut path: Vec = Vec::new(); + Self::backtracking(&mut result, &mut path, &nums, 0); + result + } +} +``` + ### C ```c