77.Combinations

class Solution {
    public List<List<Integer>> combine(int n, int k) {
        List<List<Integer>> ans = new ArrayList<>();
        helper(ans, new ArrayList<>(), 1, n, k);
        return ans;
    }

    public void helper(List<List<Integer>> ans, List<Integer> comb, int start, int n, int k){
        if(k == 0){
            ans.add(new ArrayList<Integer>(comb));
            return;
        }
        for(int i = start; i<=n; i++){
            comb.add(i);
            helper(ans, comb, i+1, n, k - 1);
            comb.remove(comb.size() - 1);
        }
    }
}

results matching ""

    No results matching ""