LeetCode77. Combinations
来源:互联网 发布:淘宝怎么购买东西 编辑:程序博客网 时间:2024/06/06 01:58
题目链接:
https://leetcode.com/problems/combinations/
题目描述:
输出1~n的 k 个数字的所有排列组合。
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>> res; vector<int> cmb; void combine(int idx,int cnt,int k,int n){ if(cnt>k){ return; } else if(cnt==k){ res.push_back(cmb); } else{ for(int i=idx;i<=n;i++){ cmb.push_back(i); combine(i+1,cnt+1,k,n); cmb.pop_back(); } } } vector<vector<int>> combine(int n, int k) { combine(1,0,k,n); return res; }};
0 0
- LeetCode77:Combinations
- LeetCode77. Combinations
- leetcode77 Combinations
- leetcode77. Combinations
- LeetCode77 Combinations
- LeetCode77——Combinations
- [Leetcode77, medium] Combination
- Leetcode77: Linked List Cycle
- 【leetcode77】Single Number
- Combinations
- Combinations
- Combinations
- Combinations
- Combinations
- Combinations
- Combinations
- Combinations
- Combinations
- Dom15(让图片飞)
- 操作构件
- HYSBZ/BZOJ 1040 [ZJOI2008] 骑士 - 基环树&树形dp
- 深入理解JVM之内存区域与内存溢出
- 陶哲轩实分析 3.1节 习题试解
- LeetCode77. Combinations
- 操作SQL构件
- Windows 7下硬盘安装Ubuntu 13.04图文教程
- Object-C--->Swift之(十一)属性观察者
- HDU-1253 胜利大逃亡 (BFS)
- 【slighttpd】基于lighttpd架构的Server项目实战(7)—http-parser
- [Leetcode]211. Add and Search Word - Data structure design @python
- jQuery1(jQuery介绍及$)
- 猿代码邀请码