LeetCode第77题:组合(中等)
今天打卡的题是LeetCode第8题,之前做过,就又做了一道77题
- 题目:给定两个整数 n 和 k,返回 1 … n 中所有可能的 k 个数的组合。
- 解题思路:借鉴了第40题的思想,利用栈和递归方法。
class Solution {
private List<List<Integer>> ans = new ArrayList<>();
private int n;
private int k;
public List<List<Integer>> combine(int n, int k) {
if(k>n || k==0) return ans;
this.n = n;
this.k = k;
findcombine(1,n,k,new Stack<>());
return ans;
}
private void findcombine(int start,int end,int k,Stack<Integer> pre){
if(k==0){
ans.add(new ArrayList<