-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcombination_sum_ii.rs
66 lines (62 loc) · 1.88 KB
/
combination_sum_ii.rs
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
///
/// Problem: Combination Sum II
///
/// Given a collection of candidate numbers (`candidates`) and a target number (`target`), find **all unique combinations** where the chosen numbers sum to `target`.
///
/// Each number in `candidates` **can only be used once** in the combination.
///
/// **Example 1:**
/// ```plaintext
/// Input: candidates = [10,1,2,7,6,1,5], target = 8
/// Output: [[1,1,6], [1,2,5], [1,7], [2,6]]
/// ```
///
/// **Example 2:**
/// ```plaintext
/// Input: candidates = [2,5,2,1,2], target = 5
/// Output: [[1,2,2], [5]]
/// ```
///
/// **Constraints:**
/// - `1 <= candidates.length <= 100`
/// - `1 <= candidates[i] <= 50`
/// - `1 <= target <= 30`
/// - **Each number may only be used once in a combination.**
///
/// # Solution:
///
/// - **Time Complexity:** `O(2^n)`
/// - **Space Complexity:** `O(n)`
impl Solution {
pub fn combination_sum2(candidates: Vec<i32>, target: i32) -> Vec<Vec<i32>> {
let mut candidates = candidates;
candidates.sort();
let mut result = Vec::new();
let mut current_combination = Vec::new();
Self::backtrack(&candidates, target, 0, &mut current_combination, &mut result);
result
}
fn backtrack(
candidates: &Vec<i32>,
target: i32,
start: usize,
current_combination: &mut Vec<i32>,
result: &mut Vec<Vec<i32>>,
) {
if target == 0 {
result.push(current_combination.clone());
return;
}
for i in start..candidates.len() {
if i > start && candidates[i] == candidates[i - 1] {
continue;
}
if target < candidates[i] {
break;
}
current_combination.push(candidates[i]);
Self::backtrack(candidates, target - candidates[i], i + 1, current_combination, result);
current_combination.pop();
}
}
}