2021/04/14
impl Solution {
pub fn combine(n: i32, k: i32) -> Vec<Vec<i32>> {
let mut res = vec![];
Solution::combine_0(n, k, &mut vec![], &mut res);
return res;
}
fn combine_0(n: i32, k: i32, cur: &mut Vec<i32>, res: &mut Vec<Vec<i32>>) {
if cur.len() == k as usize {
res.push(cur.clone());
return;
}
// 注意这个 s,保证了结果集是升序的
let s = *cur.last().unwrap_or(&0) + 1;
for i in s..=n {
cur.push(i);
Solution::combine_0(n, k, cur, res);
cur.pop();
}
}
}