LeetCode 77. Combinations
2016-12-03 17:18
351 查看
描述
n个1~n的数,找出含有k个数的组合解决
回溯class Solution { public: vector<vector<int>> res; vector<int> tmp; vector<vector<int>> combine(int n, int k) { help(res, tmp, n, k, 1, 0); return res; } void help(vector<vector<int>> &res, vector<int> &tmp, int n, int k, int pos, int cnt) { if (cnt == k) { res.push_back(tmp); return ; } for (int i = pos; i <= n; ++i) { tmp.push_back(i); help(res, tmp, n, k, i + 1, cnt + 1); tmp.pop_back(); } return ; } };
相关文章推荐
- leetcode 179 Largest Number
- leetcode 24 Swap Nodes in Pairs
- leetcode 2 Add Two Numbers 方法1
- leetcode 2 Add Two Numbers 方法2
- leetcode刷题,没想到这么难搞!
- leetcode----Longest Substring Without Repeating Characters
- [LeetCode]47 Permutations II
- [LeetCode]65 Valid Number
- [LeetCode]123 Best Time to Buy and Sell Stock III
- [LeetCode] String Reorder Distance Apart
- [LeetCode] Sliding Window Maximum
- [LeetCode] Find the k-th Smallest Element in the Union of Two Sorted Arrays
- [LeetCode] Determine If Two Rectangles Overlap
- [LeetCode] A Distance Maximizing Problem
- leetcode_linearList
- leetcode_linearList02
- 021-Merge Two Sorted Lists(合并两个排好序的单链表);leetcode
- LeetCode[Day 1] Two Sum 题解
- LeetCode[Day 2] Median of Two Sorted Arrays 题解
- LeetCode[Day 3] Longest Substring Without... 题解