LeetCode-77-组合 发表于 2020-03-17 | 分类于 LeetCode | 评论数: | 热度: ℃ 本文字数: 3.5k | 阅读时长 ≈ 3 分钟 给定两个整数 n 和 k,返回 1 … n 中所有可能的 k 个数的组合。 示例: 12345678910输入: n = 4, k = 2输出:[ [2,4], [3,4], [2,3], [1,2], [1,3], [1,4],] Code1234567891011121314151617181920212223242526//time:O(n!)//space:O(k)class Solution { public: vector<vector<int>> combine(int n, int k) { vector<vector<int>> res; vector<int> temp; //数字从1开始 DFS(1,res,temp,k,n); return res; } void DFS(int index,vector<vector<int>>& res, vector<int>& temp,int k,int n){ //k表示剩下几位需要填充数字 if (k == 0) { res.push_back(temp); return; } //观察都为升序1-2、1-3、1-4这种,2-1这种被剪枝了,最小就从i开始 for (int i = index;i <= n;i++) { temp.push_back(i); DFS(i + 1, res, temp, k - 1, n); temp.pop_back(); } }}; 相关文章推荐 LeetCode-130-被围绕的区域 LeetCode-131-分割回文串 LeetCode-200-岛屿数量 LeetCode-216-组合总和III LeetCode-40-组合总和II ----\(˙<>˙)/----赞赏一下吧~ 打赏 微信支付 支付宝 本文作者: wicherQAQ 本文链接: https://wicherqaq.github.io/2020/03/17/LeetCode-77-%E7%BB%84%E5%90%88/ 版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明出处!