MCPcopy Create free account
hub / github.com/StudyRust/leetcode_rust / backtrace

Method backtrace

216-combination-sum-iii.rs:5–15  ·  view source on GitHub ↗
(n: i32, k: usize, start_index: i32, path: &mut Vec<i32>, ret: &mut Vec<Vec<i32>>, sum: i32)

Source from the content-addressed store, hash-verified

3// 回溯 = 递归 + 循环
4impl Solution {
5 pub fn backtrace(n: i32, k: usize, start_index: i32, path: &mut Vec<i32>, ret: &mut Vec<Vec<i32>>, sum: i32) {
6 if path.len() == k && path.iter().sum::<i32>() == sum {
7 ret.push(path.to_vec());
8 return;
9 }
10 for i in start_index..=n {
11 path.push(i);
12 Self::backtrace(n, k, i+1, path, ret, sum);
13 path.pop();
14 }
15 }
16
17 pub fn combination_sum3(k: i32, n: i32) -> Vec<Vec<i32>> {
18 let mut path = vec![];

Callers

nothing calls this directly

Calls 2

pushMethod · 0.45
popMethod · 0.45

Tested by

no test coverage detected