Given A collection of numbers that might contain duplicates, return all possible unique permutations.
For example,
[1,1,2]The following unique permutations:
[1,1,2], [1,2,1] , and [2,1,1] .
Analysis: Ideas are similar to permutations.
The backtracking thought with pruning function was used.
To follow a de-duplication rule in exchange-- the full permutation of the weight is the number of each number from the first digit to the non-recurring digital exchange after it. in programming, the description is that the number of the first and the number of the J is exchanged, the requirement [I,J] does not have the numbers equal to the number of J. That is, before each exchange, check whether the same number has been exchanged.
For detailed analysis please read: http://blog.csdn.net/xx77009833/article/details/17843415
classSolution { Public: Vector<vector<int>> Permuteunique (vector<int>&nums) {Vector<vector<int> >result; if(Nums.empty ())returnresult; Permuteuniquerecur (Nums,0, Nums.size ()-1, result); returnresult; } BOOLNoswap (vector<int>& Nums,intStartintend) { for(inti = start; I < end; i++) { if(Nums[i] = =Nums[end])return true; } return false; } voidPermuteuniquerecur (vector<int>& Nums,intStartintEnd, vector<vector<int> >&result) { if(Start = =end) {Result.push_back (nums); return; } Else { for(inti = start; I <= end; i++) { if(Noswap (nums, start, I))//Pruning functionContinue; Swap (Nums[i], Nums[start]); Permuteuniquerecur (nums, start+1, end, result); Swap (Nums[i], Nums[start]); } } }};
[Leetcode] Permutations II