-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path39-Combination_Sum.rs
More file actions
33 lines (29 loc) · 903 Bytes
/
Copy path39-Combination_Sum.rs
File metadata and controls
33 lines (29 loc) · 903 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
impl Solution {
pub fn combination_sum(candidates: Vec<i32>, target: i32) -> Vec<Vec<i32>> {
let mut result = Vec::new();
let mut current = Vec::new();
fn backtrack(
candidates: &Vec<i32>,
target: i32,
start: usize,
current: &mut Vec<i32>,
result: &mut Vec<Vec<i32>>,
) {
if target == 0 {
result.push(current.clone());
return;
}
if target < 0 {
return;
}
for i in start..candidates.len() {
let num = candidates[i];
current.push(num);
backtrack(candidates, target - num, i, current, result);
current.pop();
}
}
backtrack(&candidates, target, 0, &mut current, &mut result);
result
}
}