|
| 1 | +/** |
| 2 | + * [40] 组合总和 II |
| 3 | + * |
| 4 | + * 给定一个候选人编号的集合 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合。 |
| 5 | + * candidates 中的每个数字在每个组合中只能使用 一次 。 |
| 6 | + * 注意:解集不能包含重复的组合。 |
| 7 | + * |
| 8 | + * 示例 1: |
| 9 | + * |
| 10 | + * 输入: candidates = [10,1,2,7,6,1,5], target = 8, |
| 11 | + * 输出: |
| 12 | + * [ |
| 13 | + * [1,1,6], |
| 14 | + * [1,2,5], |
| 15 | + * [1,7], |
| 16 | + * [2,6] |
| 17 | + * ] |
| 18 | + * 示例 2: |
| 19 | + * |
| 20 | + * 输入: candidates = [2,5,2,1,2], target = 5, |
| 21 | + * 输出: |
| 22 | + * [ |
| 23 | + * [1,2,2], |
| 24 | + * [5] |
| 25 | + * ] |
| 26 | + * |
| 27 | + * 提示: |
| 28 | + * |
| 29 | + * 1 <= candidates.length <= 100 |
| 30 | + * 1 <= candidates[i] <= 50 |
| 31 | + * 1 <= target <= 30 |
| 32 | + * |
| 33 | + */ |
| 34 | +pub struct Solution {} |
| 35 | + |
| 36 | +// problem: https://leetcode.cn/problems/combination-sum-ii/ |
| 37 | +// discuss: https://leetcode.cn/problems/combination-sum-ii/discuss/?currentPage=1&orderBy=most_votes&query= |
| 38 | + |
| 39 | +// submission codes start here |
| 40 | + |
| 41 | +impl Solution { |
| 42 | + pub fn combination_sum2(candidates: Vec<i32>, target: i32) -> Vec<Vec<i32>> { |
| 43 | + let mut candidates = candidates.clone(); |
| 44 | + candidates.sort(); |
| 45 | + |
| 46 | + let mut res = vec![]; |
| 47 | + |
| 48 | + let mut sum = candidates[0]; |
| 49 | + let mut curr = vec![candidates[0]]; |
| 50 | + let mut curr_idx = vec![0]; |
| 51 | + let mut last_idx = 0; |
| 52 | + |
| 53 | + 'outer: while curr.len() > 0 { |
| 54 | + // println!("{:?}", curr); |
| 55 | + if sum == target { |
| 56 | + res.push(curr.clone()); |
| 57 | + } |
| 58 | + if sum < target { |
| 59 | + let i = last_idx + 1; |
| 60 | + if i < candidates.len() { |
| 61 | + last_idx = i; |
| 62 | + curr_idx.push(i); |
| 63 | + curr.push(candidates[i]); |
| 64 | + sum += candidates[i]; |
| 65 | + continue; |
| 66 | + } |
| 67 | + } |
| 68 | + 'poping: while let Some(v) = curr.pop() { |
| 69 | + last_idx = curr_idx.pop().unwrap(); |
| 70 | + sum -= v; |
| 71 | + |
| 72 | + let mut nexti = last_idx; |
| 73 | + |
| 74 | + while candidates[nexti] == v { |
| 75 | + // println!("nexti: {}", nexti); |
| 76 | + nexti += 1; |
| 77 | + if nexti == candidates.len() { |
| 78 | + continue 'poping; |
| 79 | + } |
| 80 | + } |
| 81 | + last_idx = nexti; |
| 82 | + curr_idx.push(last_idx); |
| 83 | + curr.push(candidates[last_idx]); |
| 84 | + sum += candidates[last_idx]; |
| 85 | + continue 'outer; |
| 86 | + } |
| 87 | + } |
| 88 | + res |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +// submission codes end |
| 93 | + |
| 94 | +#[cfg(test)] |
| 95 | +mod tests { |
| 96 | + use super::*; |
| 97 | + |
| 98 | + #[test] |
| 99 | + fn test_40() { |
| 100 | + assert_eq!( |
| 101 | + Solution::combination_sum2(vec![2, 5, 2, 1, 2], 5), |
| 102 | + vec![vec![1, 2, 2], vec![5]], |
| 103 | + ); |
| 104 | + assert_eq!( |
| 105 | + Solution::combination_sum2(vec![10, 1, 2, 7, 6, 1, 5], 8), |
| 106 | + vec![vec![1, 1, 6], vec![1, 2, 5], vec![1, 7], vec![2, 6]], |
| 107 | + ) |
| 108 | + } |
| 109 | +} |
0 commit comments