代码随想录算法训练营第二十四天| 77. 组合

77.组合

public  List> combine(int n, int k) {
    if (n < k) {
        return null;
    }
    List> list = new ArrayList<>();
    List path = new ArrayList<>();
    backSource(n, path, list, k);
    return list;

}

public  void backSource(int n, List path, List> list, int k) {
    if (path.size() > k) {
        return;
    }

    if (path.size() == k) {
        list.add(new ArrayList<>(path));
        return;
    }

    for (int i = 1; i <= n; i++) {
        if (!path.contains(i)) {
            path.add(i);
            backSource(i, path, list, k);
            path.remove(path.size() - 1);
        }

    }

}

你可能感兴趣的:(算法,windows)