-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path40-Combination_Sum_II.rs
More file actions
40 lines (33 loc) · 1.03 KB
/
Copy path40-Combination_Sum_II.rs
File metadata and controls
40 lines (33 loc) · 1.03 KB
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
34
35
36
37
38
39
40
impl Solution {
pub fn combination_sum2(mut candidates: Vec<i32>, target: i32) -> Vec<Vec<i32>> {
candidates.sort();
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() {
if i > start && candidates[i] == candidates[i - 1] {
continue;
}
let num = candidates[i];
current.push(num);
backtrack(candidates, target - num, i + 1, current, result);
current.pop();
}
}
backtrack(&candidates, target, 0, &mut current, &mut result);
result
}
}