77.组合
class Solution {
public List<List<Integer>> combine(int n, int k) {
List<List<Integer>> res = new LinkedList<>();
Stack<Integer> stack = new Stack<>();
process(res, stack, 1, n, k);
return res;
}
public void process(List<List<Integer>> res, Stack<Integer> stack, int i, int n, int k) {
if (stack.size() == k) {
List<Integer> linkedList = new LinkedList<>();
Integer[] integers = stack.toArray(new Integer[stack.size()]);
linkedList = Arrays.asList(integers);
res.add(linkedList);
return;
}
for (int j = i; j <= n; j++) {
stack.add(j);
process(res, stack, j + 1,n, k);
stack.pop();
}
}
}