From 90ff8a8ff85752d865a458d9ff68c41ce9e3de21 Mon Sep 17 00:00:00 2001 From: cezarbbb <105843128+cezarbbb@users.noreply.github.com> Date: Wed, 20 Jul 2022 15:00:40 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200491.=E9=80=92?= =?UTF-8?q?=E5=A2=9E=E5=AD=90=E5=BA=8F=E5=88=97=20Rust=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 添加 0491.递增子序列 Rust版本 --- problems/0491.递增子序列.md | 51 ++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) 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 From dcb4ff10a36ef93f2f2d0f099df2c3e8d912421a Mon Sep 17 00:00:00 2001 From: cezarbbb <105843128+cezarbbb@users.noreply.github.com> Date: Fri, 22 Jul 2022 09:56:34 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200047.=E5=85=A8?= =?UTF-8?q?=E6=8E=92=E5=88=97II=20Rust=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 添加 0047.全排列II Rust版本 --- problems/0047.全排列II.md | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/problems/0047.全排列II.md b/problems/0047.全排列II.md index 88680c8c..2c3f579f 100644 --- a/problems/0047.全排列II.md +++ b/problems/0047.全排列II.md @@ -351,6 +351,40 @@ func permuteUnique(_ nums: [Int]) -> [[Int]] { } ``` +### Rust + +```Rust +impl Solution { + fn backtracking(result: &mut Vec>, path: &mut Vec, nums: &Vec, used: &mut Vec) { + let len = nums.len(); + if path.len() == len { + result.push(path.clone()); + return; + } + for i in 0..len { + if i > 0 && nums[i] == nums[i - 1] && used[i - 1] == false { continue; } + if used[i] == false { + used[i] = true; + path.push(nums[i]); + Self::backtracking(result, path, nums, used); + path.pop(); + used[i] = false; + } + } + } + + pub fn permute_unique(nums: Vec) -> Vec> { + let mut result: Vec> = Vec::new(); + let mut path: Vec = Vec::new(); + let mut used = vec![false; nums.len()]; + let mut nums= nums; + nums.sort(); + Self::backtracking(&mut result, &mut path, &nums, &mut used); + result + } +} +``` + ### C ```c //临时数组