update 0077.组合优化.md 关于 rust 的一些优化

This commit is contained in:
fw_qaq
2022-12-17 23:04:18 +08:00
committed by GitHub
parent 3c110bd4c3
commit e1d0a28bdd

View File

@ -267,22 +267,22 @@ Rust:
```Rust ```Rust
impl Solution { impl Solution {
fn backtracking(result: &mut Vec<Vec<i32>>, path: &mut Vec<i32>, n: i32, k: i32, startIndex: i32) { fn backtracking(result: &mut Vec<Vec<i32>>, path: &mut Vec<i32>, n: i32, k: i32, start_index: i32) {
let len= path.len() as i32; let len= path.len() as i32;
if len == k{ if len == k{
result.push(path.to_vec()); result.push(path.to_vec());
return; return;
} }
// 此处剪枝 // 此处剪枝
for i in startIndex..= n - (k - len) + 1 { for i in start_index..= n - (k - len) + 1 {
path.push(i); path.push(i);
Self::backtracking(result, path, n, k, i+1); Self::backtracking(result, path, n, k, i+1);
path.pop(); path.pop();
} }
} }
pub fn combine(n: i32, k: i32) -> Vec<Vec<i32>> { pub fn combine(n: i32, k: i32) -> Vec<Vec<i32>> {
let mut result: Vec<Vec<i32>> = Vec::new(); let mut result = vec![];
let mut path: Vec<i32> = Vec::new(); let mut path = vec![];
Self::backtracking(&mut result, &mut path, n, k, 1); Self::backtracking(&mut result, &mut path, n, k, 1);
result result
} }