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