-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.rs
63 lines (57 loc) · 1.72 KB
/
main.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
fn main() {
Solution::combination_sum2(vec![10,1,2,7,6,1,5], 8);
}
struct Solution {}
impl Solution {
pub fn combination_sum2(mut candidates: Vec<i32>, target: i32) -> Vec<Vec<i32>> {
candidates.sort_unstable();
let mut buf = vec![];
let mut target_buf = vec![];
Self::sum(&candidates, target, &mut buf, &mut target_buf);
println!("buf = {:#?}", target_buf);
target_buf
}
pub fn sum( candidates: &[i32],
target: i32,
buf: &mut Vec<i32>,
target_buf: &mut Vec<Vec<i32>> ) {
if target == 0 { target_buf.push(buf.clone()) }
if target < 0 { return }
if candidates.len() == 0 { return }
// println!(" -> target = {:#?}", target);
let mut e = candidates.len() - 1;
while e > 0 && candidates[e] > target { e -= 1; }
let mut prev = -1;
for i in 0..=e {
if candidates[i] == prev { continue }
// println!("testing candidates[i] = {:#?}", candidates[i]);
buf.push(candidates[i]);
Self::sum(&candidates[i+1..], target - candidates[i], buf, target_buf);
prev = buf.pop().unwrap();
}
// println!(" <- ");
}
}
#[cfg(test)]
mod test {
use crate::*;
#[test]
fn basic() {
assert_eq!(Solution::combination_sum2(
vec![10,1,2,7,6,1,5], 8),
vec![
vec![1, 1, 6],
vec![1, 2, 5],
vec![1, 7],
vec![2, 6],
]
);
assert_eq!(Solution::combination_sum2(
vec![2,5,2,1,2], 5),
vec![
vec![1,2,2],
vec![5]
]
);
}
}