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].
To find the entire array of elements, the array does not contain duplicate elements
Algorithm 1: Recursion
Similar to DFS recursion. For an array of n elements, first determine the first position of the element, the first one is possible in n (each of the following elements and the first element Exchange), and then the array [2...N] of the entire arrangement. As a result of the total number of a series of n! Arrangement, so the time complexity is O (n! )
Class Solution {public
:
vector<vector<int> > Permute (vector<int> &num) {
vector <vector<int> > Res;
if (num.size () = = 0) return res;
Vector<int> Tmpres;
Permuterecur (num, 0, res, tmpres);
return res;
}
void Permuterecur (vector<int> &num, int index, vector<vector<int> >&res, vector<int> &tmpres)
{
if (index = = num.size ())
{
res.push_back (tmpres);
return;
}
for (int i = index; i < num.size (); i++)
{
swap (Num[index], num[i]);
Tmpres.push_back (Num[index]);
Permuterecur (num, index+1, res, tmpres);
Tmpres.pop_back ();
Swap (Num[index], num[i]);
}
}
;
Permutations II
Given A collection of numbers that might contain duplicates and 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]
.
To find the entire array of elements, the array contains duplicate elements
Analysis: To avoid duplication, it is necessary to ensure that we enumerate the elements of a certain location, we cannot enumerate the duplicates.
Algorithm 2:
On the basis of algorithm 1, when we enumerate the elements in the first position, to exchange the following J elements and I, we first guarantee that there are no elements in the [i...j-1] range that are the same as position J. There are two approaches (1) that can be sequentially searched each time they need to be exchanged, (2) using a hash table to check the weight. See the code below for details.
Be careful not to assume that the following two methods can be weighed: (1) The first array is sorted, and it is wrong to make sure that the element that is currently being exchanged differs from the previous one, even though the order is started. But the exchange of elements causes the array to become unordered again (2) It is also wrong to sort the sub array starting at the current index each time it enters the recursive function permuterecur, because each time the element is exchanged, we want to restore the exchanged elements, if sorted within the recursive function, You cannot recover the exchanged elements correctly.
Class Solution {public:vector<vector<int> > Permuteunique (vector<int> &num) {vector&
lt;vector<int> > Res;
if (num.size () = = 0) return res;
Vector<int> Tmpres;
Permuterecur (num, 0, res, tmpres);
return res; } void Permuterecur (vector<int> &num, int index, vector<vector<int> >&res, vector&
Lt;int>&tmpres) {if (index = = num.size ()) {res.push_back (tmpres);
Return
for (int i = index; i < num.size (); i++) if (i = = Index | |!find (NUM, index, I, num[i))
{Swap (Num[index], num[i]);
Tmpres.push_back (Num[index]);
Permuterecur (num, index+1, res, tmpres);
Tmpres.pop_back ();
Swap (Num[index], num[i]); ///Look for elements from the [start,end) range of the array target bool Find (vector<int> &num, int StArt, int end, int target) {for (int i = START I < end; i++) if (num[i] = = target)
return true;
return false; }
};
class Solution {public:vector<vector<int> > Permuteunique (vector<int> &num) {ve
ctor<vector<int> > Res;
if (num.size () = = 0) return res;
Vector<int> Tmpres;
Permuterecur (num, 0, res, tmpres);
return res; } void Permuterecur (vector<int> &num, int index, vector<vector<int> >&res, vector&
Lt;int>&tmpres) {if (index = = num.size ()) {res.push_back (tmpres);
Return
} unordered_set<int> Umap; for (int i = index; i < num.size (); i++) if (Umap.find (num[i)) = = Umap.end ()) {u
Map.insert (Num[i]);
Swap (Num[index], num[i]);
Tmpres.push_back (Num[index]);
Permuterecur (num, index+1, res, tmpres);
Tmpres.pop_back ();
Swap (Num[index], num[i]); }
}
};