[LeetCode]Combinations
来源:互联网 发布:做淘宝能赚钱吗 编辑:程序博客网 时间:2024/05/01 15:34
class Solution {//using recurrence to get these combinationspublic:vector<vector<int> > combine(int n, int k) {// Start typing your C/C++ solution below// DO NOT write int main() functionvector<int> temp;vector<vector<int> > ans;combine_rec(ans , temp , n , k , 1);return ans;}void combine_rec(vector<vector<int> > &ans , vector<int>& temp , int n , int k , int index) {if (k == 0)ans.push_back(temp);else if (k > 0 && index <= n){temp.push_back(index);combine_rec(ans , temp , n , k-1 , index+1);temp.pop_back();combine_rec(ans , temp , n , k , index+1);}}};
second time
class Solution {public: void combineUtil(int n, int k, int curIdx, vector<int>& curPath, vector<vector<int> >& allPath) { if(curPath.size() == k) { allPath.push_back(curPath); return; } if(curIdx >= n+1) return; combineUtil(n, k, curIdx+1, curPath, allPath); curPath.push_back(curIdx); combineUtil(n, k, curIdx+1, curPath, allPath); curPath.pop_back(); } vector<vector<int> > combine(int n, int k) { // Start typing your C/C++ solution below // DO NOT write int main() function vector<int> curPath; vector<vector<int> > allPath; combineUtil(n, k, 1, curPath, allPath); return allPath; }};
- 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
- ServletOutputStream回写页面乱码
- php 字符串函数
- Android应用开发中的风格和主题(style,themes)
- Android 查看内存
- SVM中令 WX + b = 1的理解
- [LeetCode]Combinations
- hdu 3729 I'm Telling the Truth(二分图最大匹配)
- 加载XML时chrome中document不能load的问题
- zoj1005 jugs 广搜,错了好多次。。细节啊!!
- [LeetCode]Construct Binary Tree from Inorder and Postorder Traversal
- 初识遗传算法
- [LeetCode]Construct Binary Tree from Preorder and Inorder Traversal
- 如何在Win7系统下硬盘安装Win8
- Android AlarmManager 详解