From aaac4825ec0f638b07e6772829bc25a26f677863 Mon Sep 17 00:00:00 2001 From: cezarbbb <105843128+cezarbbb@users.noreply.github.com> Date: Mon, 18 Jul 2022 09:46:46 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200078.=E5=AD=90=E9=9B=86=20?= =?UTF-8?q?Rust=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 添加 0078.子集 Rust版本 --- problems/0078.子集.md | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/problems/0078.子集.md b/problems/0078.子集.md index e6cc668b..3e98311e 100644 --- a/problems/0078.子集.md +++ b/problems/0078.子集.md @@ -292,6 +292,30 @@ function subsets(nums: number[]): number[][] { }; ``` +## Rust + +```Rust +impl Solution { + fn backtracking(result: &mut Vec>, path: &mut Vec, nums: &Vec, start_index: usize) { + result.push(path.clone()); + let len = nums.len(); + // if start_index >= len { return; } + for i in start_index..len { + path.push(nums[i]); + Self::backtracking(result, path, nums, i + 1); + path.pop(); + } + } + + pub fn subsets(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