Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[LeetCode] 77. Combinations #77

Open
grandyang opened this issue May 30, 2019 · 0 comments
Open

[LeetCode] 77. Combinations #77

grandyang opened this issue May 30, 2019 · 0 comments

Comments

@grandyang
Copy link
Owner

grandyang commented May 30, 2019


请点击下方图片观看讲解视频
Click below image to watch YouTube Video
Video

Given two integers n and k, return all possible combinations of k numbers chosen from the range [1, n].

You may return the answer in any order.

Example 1:

Input: n = 4, k = 2
Output: [[1,2],[1,3],[1,4],[2,3],[2,4],[3,4]]
Explanation: There are 4 choose 2 = 6 total combinations.
Note that combinations are unordered, i.e., [1,2] and [2,1] are considered to be the same combination.

Example 2:

Input: n = 1, k = 1
Output: [[1]]
Explanation: There is 1 choose 1 = 1 total combination.

Constraints:

  • 1 <= n <= 20
  • 1 <= k <= n

这道题让求1到n共n个数字里k个数的组合数的所有情况,还是要用深度优先搜索 DFS 来解,根据以往的经验,像这种要求出所有结果的集合,一般都是用 DFS 调用递归来解。这里建立一个保存最终结果的大集合 res,还要定义一个保存每一个组合的小集合 cur,每次放一个数到 cur 里,如果 cur 里数个数到了k个,则把 cur 保存到最终结果中,否则在下一层中继续调用递归。根据上面分析,可写出代码如下:

解法一:

class Solution {
public:
    vector<vector<int>> combine(int n, int k) {
        vector<vector<int>> res;
        vector<int> cur;
        dfs(n, k, 1, cur, res);
        return res;
    }
    void dfs(int n, int k, int level, vector<int>& cur, vector<vector<int>>& res) {
        if (cur.size() == k) {
            res.push_back(cur); 
            return;
        }
        for (int i = level; i <= n; ++i) {
            cur.push_back(i);
            dfs(n, k, i + 1, cur, res);
            cur.pop_back();
        }
    }
};

对于n = 5, k = 3, 处理的结果如下:

1 2 3
1 2 4
1 2 5
1 3 4
1 3 5
1 4 5
2 3 4
2 3 5
2 4 5
3 4 5

再来看一种递归的写法,此解法没用子函数当递归函数,而是把本身就当作了递归函数,写起来十分的简洁,也是非常有趣的一种解法。这个解法用到了一个重要的性质 C(n, k) = C(n-1, k-1) + C(n-1, k),这应该在我们高中时候学排列组合的时候学过吧,博主也记不清了。总之,翻译一下就是,在n个数中取k个数的组合项个数,等于在 n-1 个数中取 k-1 个数的组合项个数再加上在 n-1 个数中取k个数的组合项个数之和。这里博主就不证明了,因为博主也不会,就直接举题目中的例子来说明吧:

C(4, 2) = C(3, 1) + C(3, 2)

我们不难写出 C(3, 1) 的所有情况:[1], [2], [3],还有 C(3, 2) 的所有情况:[1, 2], [1, 3], [2, 3]。可以发现二者加起来为6,正好是 C(4, 2) 的个数之和。但是仔细看会发现,C(3, 2)的所有情况包含在 C(4, 2) 之中,但是 C(3, 1) 的每种情况只有一个数字,而需要的结果 k=2,其实很好办,每种情况后面都加上4,于是变成了:[1, 4], [2, 4], [3, 4],加上 C(3, 2) 的所有情况:[1, 2], [1, 3], [2, 3],正好就得到了 n=4, k=2 的所有情况了。参见代码如下:

解法二:

class Solution {
public:
    vector<vector<int>> combine(int n, int k) {
        if (k > n || k < 0) return {};
        if (k == 0) return {{}};
        vector<vector<int>> res = combine(n - 1, k - 1);
        for (auto &a : res) a.push_back(n);
        for (auto &a : combine(n - 1, k)) res.push_back(a);
        return res;
    }
};

再来看一种迭代的写法,也是一种比较巧妙的方法。这里每次先递增最右边的数字,存入结果 res 中,当右边的数字超过了n,则增加其左边的数字,然后将当前数组赋值为左边的数字,再逐个递增,直到最左边的数字也超过了n,停止循环。对于 n=4, k=2 时,遍历的顺序如下所示:

0 0 #initialization
1 0
1 1 
1 2 #push_back
1 3 #push_back
1 4 #push_back
1 5
2 5
2 2 
2 3 #push_back
2 4 #push_back
...
3 4 #push_back
3 5
4 5
4 4
4 5
5 5 #stop   

解法三:

class Solution {
public:
    vector<vector<int>> combine(int n, int k) {
        vector<vector<int>> res;
        vector<int> cur(k);
        int i = 0;
        while (i >= 0) {
            ++cur[i];
            if (cur[i] > n) {
                --i;
            } else if (i == k - 1) {
                res.push_back(cur);
            } else {
                ++i;
                cur[i] = cur[i - 1];
            }
        }
        return res;
    }
};

Github 同步地址:

#77

类似题目:

Combination Sum

Permutations

参考资料:

https://leetcode.com/problems/combinations

https://leetcode.com/problems/combinations/discuss/27015/3-ms-Java-Solution

https://leetcode.com/problems/combinations/discuss/27002/Backtracking-Solution-Java

https://leetcode.com/problems/combinations/discuss/26992/Short-Iterative-C++-Answer-8ms

LeetCode All in One 题目讲解汇总(持续更新中...)

(欢迎加入博主的知识星球,博主将及时答疑解惑,并分享刷题经验与总结,快快加入吧~)

知识星球 喜欢请点赞,疼爱请打赏❤️~.~

微信打赏

|

Venmo 打赏


---|---

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant