1.1题目:组合
给定两个整数 n 和 k,返回 1 ... n 中所有可能的 k 个数的组合。
示例:
输入: n = 4, k = 2
输出:
[
[2,4],
[3,4],
[2,3],
[1,2],
[1,3],
[1,4],
]
解决代码:
class Solution {
int n;
int k;
List> res = new LinkedList<>();
public List> combine(int n, int k) {
this.n = n;
this.k = k;
middleMeth(1,new LinkedList());
return res;
}
public void middleMeth(int temp,LinkedList list){
if(list.size() == k){
res.add(new LinkedList(list));
}
for(int i = temp; i < n + 1 ;++i){
list.add(i);
middleMeth(i + 1,list);
list.removeLast();
}
}
}
1.2题目:子集
给定一