[LeetCode] Combinations
来源:互联网 发布:阿里云按量付费转包月 编辑:程序博客网 时间:2024/05/10 22:27
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],]
思路:很简单的递归问题。
vector<vector<int> > combine(int n, int k) { int pos = 1; vector<int> comb; vector<vector<int>> combs; combineUtil(n, k, pos, comb, combs ); return combs;}void combineUtil(int n, int k, int pos, vector<int> comb, vector<vector<int>>& combs ){// n: 1,2,..., n// k: select k numbers from [1,n]// pos: the number from [1,n] that may be selected. // This is the depth of the state stack.// comb: the selected numbers will be stored in comb.// combs: a completed combination will be stored in combs. if(comb.size()==k) { combs.push_back(comb); return; } if( n-pos+1<k-comb.size() ) return; comb.push_back(pos); pos++; combineUtil(n, k, pos, comb, combs ); comb.pop_back(); combineUtil(n, k, pos, comb, combs );}
0 0
- LeetCode: Combinations
- LeetCode: Combinations
- LeetCode Combinations
- [Leetcode] Combinations
- [LeetCode] Combinations
- Leetcode: Combinations
- LeetCode Combinations
- [Leetcode] Combinations
- [Leetcode] Combinations
- [leetcode] Combinations
- [LeetCode]Combinations
- [leetcode]Combinations
- LeetCode-Combinations
- [leetcode] Combinations
- LeetCode - Combinations
- 【Leetcode】Combinations
- Leetcode: Combinations
- 【leetcode】Combinations
- 未发生的发生大声道
- 别来看望我
- 风松岛撒旦法的说法
- 如果你能尊重我的选择
- 别人都以为你自己没事呢
- [LeetCode] Combinations
- 希望你自己真的明白什么叫持之以恒
- 我不知道是不是正确的
- 都他们神经病么
- 还是得继续
- 不搞就真的废了,不得不高啊
- 说多了都是泪
- 说那么多也没一点用
- 如果我到了三级是不是就可以带链接?