- Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcombination-sum.rs
41 lines (35 loc) · 1.05 KB
/
combination-sum.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
#![allow(dead_code, unused, unused_variables)]
fnmain(){}
structSolution;
implSolution{
pubfncombination_sum(candidates:Vec<i32>,target:i32) -> Vec<Vec<i32>>{
letmut candidates = candidates;
candidates.sort();
Self::calc(&candidates, target)
}
fncalc(candidates:&Vec<i32>,target:i32) -> Vec<Vec<i32>>{
letmut r = vec![];
for&i in candidates.iter(){
if i > target {
break;
}
letmut v = vec![];
if i == target {
v.push(i);
r.push(v);
}elseif i < target {
let x = Self::calc(&candidates, target - i);
formut m in x {
if !m.is_empty(){
if i >= *m.last().unwrap(){
// 递增排列,用于去重复
m.push(i);
r.push(m);
}
}
}
}
}
r
}
}