[LeetCode]Combinations
来源:互联网 发布:mac屏幕截图保存位置 编辑:程序博客网 时间:2024/06/07 11:23
题目
Number: 77
Difficulty: Medium
Tags: Backtracking
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],]
题解
给出两个整数k和n,在1...n
中找出所有k个数的组合。
用回溯法求解。
代码
vector<vector<int>> combine(int n, int k) { vector<vector<int>> res; if(n < k) return res; vector<int> ans; com(res, ans, n, k, 0); return res;}void com(vector<vector<int>> &res, vector<int> &ans, int n, int k, int start) { if(ans.size() == k){ res.push_back(ans); return; } for(int i = start; i < n; i++){ ans.push_back(i + 1); com(res, ans, n, k, i + 1); ans.pop_back(); }}
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
- ubuntu 12.04 KDE(Kool Desktop Environment,桌面环境)的安装
- [LeetCode]Sort Colors
- 闲话js前端框架(5)——再看自己一年前设计的微型渲染引擎
- 暴雪总裁总结游戏十条经验
- 抓住移动竞价蓝海,分享移动互联网SEM竞价干货
- [LeetCode]Combinations
- xcode7发送session请求的时候报错解决
- 算法初探——分治法
- HDOJ 1953 Bridging signals (STL,lower_bound)
- [LeetCode]Subsets
- hdu1599 find the mincost route
- 树莓派无线网卡配置
- Binary Tree Paths
- [LeetCode]Word Search