Day27.算法训练

77. 组合

class Solution {
    List> result = new ArrayList<>();

    public List> combine(int n, int k) {

        combine(n, k, 1, new ArrayList<>());

        return result;

    }

    public void combine(int n, int k, int start, List path) {
        if (path.size() == k) {
            result.add(new ArrayList<>(path));
            return;
        }

        for (int i = start; i <= n && ((n - i + 1) >= k - path.size()); i++) {
            path.add(i);
            combine(n, k, i + 1, path);
            path.remove(path.size() - 1);
        }
    }
}

你可能感兴趣的:(leetcode,算法,数据结构)