-
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)]
fn main() {}
struct Solution;
impl Solution {
pub fn combination_sum(candidates: Vec<i32>, target: i32) -> Vec<Vec<i32>> {
let mut candidates = candidates;
candidates.sort();
Self::calc(&candidates, target)
}
fn calc(candidates: &Vec<i32>, target: i32) -> Vec<Vec<i32>> {
let mut r = vec![];
for &i in candidates.iter() {
if i > target {
break;
}
let mut v = vec![];
if i == target {
v.push(i);
r.push(v);
} else if i < target {
let x = Self::calc(&candidates, target - i);
for mut m in x {
if !m.is_empty() {
if i >= *m.last().unwrap() {
// 递增排列,用于去重复
m.push(i);
r.push(m);
}
}
}
}
}
r
}
}