求全排列
交换+深度优先遍历
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].
思路:
典型的递归问题。
- 生成[2, 3]的全排列[2, 3]和[3, 2],然后把1加上去生成[1, 2, 3]和[1, 3, 2]。
- 交换1和2的位置,生成[1, 3]的全排列[1, 3]和[3, 1],然后把2加上去生成[2, 1, 3]和[2, 3, 1]。
- 在第二步的基础上交换2和3的位置,生成[2, 1]的全排列[2, 1]和[1, 2],然后把3加上去生成[3, 2, 1]和[3, 1, 2]。
public:
void internalPermute(vector<int> &num, int index, vector<int> &perm, vector<vector<int> > &result) {
int size = num.size();
if (size == index) {
result.push_back(perm);
}
else {
for (int i = index; i < size; ++i) {
swap(num[index], num[i]);
perm.push_back(num[index]);
internalPermute(num, index + 1, perm, result);
perm.pop_back();
swap(num[index], num[i]);
}
}
}
vector<vector<int> > permute(vector<int> &num) {
vector<vector<int> > result;
vector<int> perm;
internalPermute(num, 0, perm, result);
return result;
}
};
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].
思路:
如果前面Permutations的问题已经解决了的话,这个问题就变得非常简单。先排个序,以[1, 1, 2]为例,第一个1处理过以后,第二个1就可以跳掉了。
class Solution {
public:
void internalpermuteUnique(vector<int> &num, int index, vector<int> &perm, vector<vector<int> > &result) {
int size = num.size();
if (size == index) {
result.push_back(perm);
}
else {
for (int i = index; i < size; ++i) {
if ((i > index) && (num[i] == num[index])) {
continue;
}
else {
swap(num[index], num[i]);
}
perm.push_back(num[index]);
internalpermuteUnique(num, index + 1, perm, result);
perm.pop_back();
}
sort(num.begin() + index, num.end());
}
}
vector<vector<int> > permuteUnique(vector<int> &num) {
vector<vector<int> > result;
vector<int> perm;
sort(num.begin(), num.end());
internalpermuteUnique(num, 0, perm, result);
return result;
}
};