题目1: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],
]
代码如下:
class Solution {
public:
vector<vector<int> > combine(int n, int k) {
vector<vector<int>> res;
vector<int> qq;
fuck(n,k,1,res,qq);
return res;
}
void fuck(int n,int k,int start,vector<vector<int>> &res,vector<int> &qq)
{
if(qq.size()==k)
res.push_back(qq);
for(int i=start;i<=n;i++)
{
qq.push_back(i);
fuck(n,k,i+1,res,qq);
qq.pop_back();
}
}
};
题目2:permutations
Given a collection of numbers, return all possible permutations.
For example,
[1,2,3]have the following permutations:
[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2], and[3,2,1].
解题思路:本题考虑已知数组中含有重复数字的可能,比如[1,1,2]这种的最后结果就是【1,2】
代码如下:
这里写代码片
题目3:permutations-ii
Given a collection of numbers that might contain duplicates, return all possible unique permutations.
For example,
[1,1,2]have the following unique permutations:
[1,1,2],[1,2,1], and[2,1,1].
代码如下: