leetcode 77. Combinations
来源:互联网 发布:为什么黑腾讯游戏知乎 编辑:程序博客网 时间:2024/06/18 18:09
题目如下:
Given two integers n and k, return all possible combinations of k numbers out of 1 ... n.
For example,
If n = 4 and k = 2, a solution is:
[ [2,4], [3,4], [2,3], [1,2], [1,3], [1,4],]递归代码如下:
class Solution {public: vector<vector<int> > combine(int n, int k) { vector<vector<int> > rslt; vector<int> path(k, 0); combine(n, k, rslt, path); return rslt; }private: void combine(int n, int k, vector<vector<int> > &rslt, vector<int> &path) { if (k == 0) { rslt.push_back(path); return; } for (int i = n; i >= 1; i--) { path[k - 1] = i; combine(i - 1, k - 1, rslt, path); } }};
78. Subsets
Given a set of distinct integers, nums, return all possible subsets.
Note: The solution set must not contain duplicate subsets.
For example,
If nums = [1,2,3]
, a solution is:
[ [3], [1], [2], [1,2,3], [1,3], [2,3], [1,2], []]
class Solution {public: vector<vector<int>> subsets(vector<int>& nums) { sort(nums.begin(), nums.end()); vector<vector<int>> subs; vector<int> sub; genSubsets(nums, 0, sub, subs); return subs; } void genSubsets(vector<int>& nums, int start, vector<int>& sub, vector<vector<int>>& subs) { subs.push_back(sub); for (int i = start; i < nums.size(); i++) { sub.push_back(nums[i]); genSubsets(nums, i + 1, sub, subs); sub.pop_back(); } }};
阅读全文
0 0
- LeetCode 77. Combinations
- [LeetCode]77.Combinations
- LeetCode --- 77. Combinations
- [Leetcode] 77. Combinations
- [leetcode] 77.Combinations
- leetCode 77.Combinations (组合)
- [leetcode] 77. Combinations
- 77. Combinations LeetCode
- <LeetCode OJ> 77. Combinations
- [LeetCode]77. Combinations
- leetcode 77. Combinations
- LeetCode *** 77. Combinations
- leetcode 77. Combinations
- LeetCode 77. Combinations
- LeetCode-77.Combinations
- LeetCode 77.Combinations
- [LeetCode]problem 77. Combinations
- [LeetCode] 77. Combinations
- 手写识别之朴素贝叶斯
- HTTP请求接口方法(POST/GET)
- spark 2.1 TaskContext
- request和session 作用域
- 瑰丽华尔兹
- leetcode 77. Combinations
- Bzoj4276 [ONTAK2015]Bajtman i Okrągły Robin
- 基本服务器宕机的主从切换配置+基于nginx进程死掉的主从切换配置
- word目录缩进
- 作为一个Java编程新手该如何学习好Java程序编程
- Android 中用到的设计模式
- Hbase与传统数据库的区别
- 场景文字检测常用方法:
- 查看mysql索引情况