From e2d16c5894b2b9abed99c42522977dac2b6e8654 Mon Sep 17 00:00:00 2001 From: BaoTaoqi <464115280@qq.com> Date: Sun, 10 Jul 2022 09:09:02 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200017.=E7=94=B5=E8=AF=9D?= =?UTF-8?q?=E5=8F=B7=E7=A0=81=E7=9A=84=E5=AD=97=E6=AF=8D=E7=BB=84=E5=90=88?= =?UTF-8?q?=200077.=E7=BB=84=E5=90=88=200077.=E7=BB=84=E5=90=88=E4=BC=98?= =?UTF-8?q?=E5=8C=96=200216.=E7=BB=84=E5=90=88=E6=80=BB=E5=92=8CIII=20Rust?= =?UTF-8?q?=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 添加 0017.电话号码的字母组合 0077.组合 0077.组合优化 0216.组合总和III Rust版本 --- problems/0017.电话号码的字母组合.md | 43 +++++++++++++++++ problems/0077.组合.md | 50 ++++++++++++++++++++ problems/0077.组合优化.md | 26 ++++++++++ problems/0216.组合总和III.md | 29 ++++++++++++ 4 files changed, 148 insertions(+) diff --git a/problems/0017.电话号码的字母组合.md b/problems/0017.电话号码的字母组合.md index e357a33b..5778e903 100644 --- a/problems/0017.电话号码的字母组合.md +++ b/problems/0017.电话号码的字母组合.md @@ -454,6 +454,49 @@ function letterCombinations(digits: string): string[] { }; ``` +## Rust + +```Rust +impl Solution { + fn backtracking(result: &mut Vec, s: &mut String, map: &[&str; 10], digits: &String, index: usize) { + let len = digits.len(); + if len == index { + result.push(s.to_string()); + return; + } + // 在保证不会越界的情况下使用unwrap()将Some()中的值提取出来 + let digit= digits.chars().nth(index).unwrap().to_digit(10).unwrap() as usize; + let letters = map[digit]; + for i in letters.chars() { + s.push(i); + Self::backtracking(result, s, &map, &digits, index+1); + s.pop(); + } + } + pub fn letter_combinations(digits: String) -> Vec { + if digits.len() == 0 { + return vec![]; + } + const MAP: [&str; 10] = [ + "", + "", + "abc", + "def", + "ghi", + "jkl", + "mno", + "pqrs", + "tuv", + "wxyz" + ]; + let mut result: Vec = Vec::new(); + let mut s: String = String::new(); + Self::backtracking(&mut result, &mut s, &MAP, &digits, 0); + result + } +} +``` + ## C ```c diff --git a/problems/0077.组合.md b/problems/0077.组合.md index fc72be15..5a4811ba 100644 --- a/problems/0077.组合.md +++ b/problems/0077.组合.md @@ -535,6 +535,56 @@ func backtrack(n,k,start int,track []int){ } ``` +### Rust + +```Rust +impl Solution { + fn backtracking(result: &mut Vec>, path: &mut Vec, n: i32, k: i32, startIndex: i32) { + let len= path.len() as i32; + if len == k{ + result.push(path.to_vec()); + return; + } + for i in startIndex..= n { + path.push(i); + Self::backtracking(result, path, n, k, i+1); + path.pop(); + } + } + pub fn combine(n: i32, k: i32) -> Vec> { + let mut result: Vec> = Vec::new(); + let mut path: Vec = Vec::new(); + Self::backtracking(&mut result, &mut path, n, k, 1); + result + } +} +``` + +剪枝 +```Rust +impl Solution { + fn backtracking(result: &mut Vec>, path: &mut Vec, n: i32, k: i32, startIndex: i32) { + let len= path.len() as i32; + if len == k{ + result.push(path.to_vec()); + return; + } + // 此处剪枝 + for i in startIndex..= n - (k - len) + 1 { + path.push(i); + Self::backtracking(result, path, n, k, i+1); + path.pop(); + } + } + pub fn combine(n: i32, k: i32) -> Vec> { + let mut result: Vec> = Vec::new(); + let mut path: Vec = Vec::new(); + Self::backtracking(&mut result, &mut path, n, k, 1); + result + } +} +``` + ### C ```c int* path; diff --git a/problems/0077.组合优化.md b/problems/0077.组合优化.md index 8d742566..e336fb75 100644 --- a/problems/0077.组合优化.md +++ b/problems/0077.组合优化.md @@ -261,6 +261,32 @@ function combine(n: number, k: number): number[][] { }; ``` +Rust: + +```Rust +impl Solution { + fn backtracking(result: &mut Vec>, path: &mut Vec, n: i32, k: i32, startIndex: i32) { + let len= path.len() as i32; + if len == k{ + result.push(path.to_vec()); + return; + } + // 此处剪枝 + for i in startIndex..= n - (k - len) + 1 { + path.push(i); + Self::backtracking(result, path, n, k, i+1); + path.pop(); + } + } + pub fn combine(n: i32, k: i32) -> Vec> { + let mut result: Vec> = Vec::new(); + let mut path: Vec = Vec::new(); + Self::backtracking(&mut result, &mut path, n, k, 1); + result + } +} +``` + C: ```c diff --git a/problems/0216.组合总和III.md b/problems/0216.组合总和III.md index 2c1bf717..0a59e216 100644 --- a/problems/0216.组合总和III.md +++ b/problems/0216.组合总和III.md @@ -411,6 +411,35 @@ function combinationSum3(k: number, n: number): number[][] { }; ``` +## Rust + +```Rust +impl Solution { + fn backtracking(result: &mut Vec>, path:&mut Vec, targetSum:i32, k: i32, mut sum: i32, startIndex: i32) { + let len = path.len() as i32; + if len == k { + if sum == targetSum { + result.push(path.to_vec()); + } + return; + } + for i in startIndex..=9 { + sum += i; + path.push(i); + Self::backtracking(result, path, targetSum, k, sum, i+1); + sum -= i; + path.pop(); + } + } + pub fn combination_sum3(k: i32, n: i32) -> Vec> { + let mut result: Vec> = Vec::new(); + let mut path: Vec = Vec::new(); + Self::backtracking(&mut result, &mut path, n, k, 0, 1); + result + } +} +``` + ## C ```c