[LeetCode] 47. Permutations II, permutationⅱ
[Question]
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]
.
[Analysis]
This question is very similar to [LeetCode] 46. Permutations. The only difference is that the element set can be repeated in this question. This brings us a problem: if we do not distinguish repeated elements, we will see repeated results in examples like {, 2. So how can we avoid such duplication? The method is to skip the call of the recursive function when repeating element loops, and only recursion is performed for the first unused element. The result of this operation will appear in the result of the first recursive function, the repeat will be skipped later. If the element before the first repeating element is not in the current result, we do not need to perform recursion. To understand this, the code is actually well modified. First, we need to sort the element set so that duplicate elements can be adjacent. Next, we need to determine the usage of duplicate elements and the previous elements by a line of code.
[Code]
/********************************** Date: * Author: SJF0115 * Subject: 47. permutations II * URL: https://oj.leetcode.com/problems/permutations-ii/* result: AC * Source: LeetCode * time complexity: O (n !) * Space complexity: O (n) * blog: * *********************************/# include <iostream> # include <algorithm> # include <map> # include <vector> using namespace std; class Solution {public: vector <int> permuteUnique (vector <int> & num) {vector <int> result; if (num. empty () {return result;} // if // sort (num. begin (), num. end (); bool visited [num. size ()]; vector <int> visitedVec; // recursive DFS (num, visited, visite DVec, result); return result;} private: void DFS (vector <int> & num, bool visited [], vector <int> & visitedVec, vector <int> & result) {// form a fully arranged if (num. size () = visitedVec. size () {result. push_back (visitedVec); return;} // if for (int I = 0; I <num. size (); ++ I) {// duplicate element if (I> 0 &&! Visited [I-1] & num [I] = num [I-1]) {continue;} // if you have not accessed if (visited [I] = false) {visited [I] = true; visitedVec. push_back (num [I]); DFS (num, visited, visitedVec, result); visitedVec. pop_back (); visited [I] = false;} // if} // for}; int main () {Solution; vector <int> num; num. push_back (1); num. push_back (1); num. push_back (2); // rearrange the vector <int> permutes = solution. permuteUnique (num); // output for (int I = 0; I <permutes. size (); I ++) {cout <"["; for (int j = 0; j <permutes [I]. size (); j ++) {cout <permutes [I] [j];} // for cout <"]" <endl;} return 0 ;}
Code 2]
/********************************** Date: * Author: SJF0115 * Subject: 47. permutations II * URL: https://oj.leetcode.com/problems/permutations-ii/* result: AC * Source: LeetCode * time complexity: O (n !) * Space complexity: O (n) * blog: * *********************************/# include <iostream> # include <algorithm> # include <map> # include <vector> using namespace std; class Solution {public: vector <int> permuteUnique (vector <int> & num) {vector <int> result; if (num. empty () {return result;} // if int count = num. size (); // sort (num. begin (), num. end (); // counts the number of each element. map <int, int> countMap; for (int I = 0; I <count; ++ I) {countMap [num [I] ++ ;}/// bind a vector <pair <int, int> nums; pair <int, int> pair; for (int I = 0; I <count; ++ I) {pair. first = num [I]; pair. second = countMap [num [I]; nums. push_back (pair);} // for vector <int> visited; // recursive DFS (nums, visited, result); return result;} private: void DFS (vector <pair <int, int> & nums, vector <int> & visited, vector <int> & result) {// form a fully arranged if (nums. size () = visited. size () {result. push_back (visited); return;} // if int numCount = nums. size (); for (int I = 0; I <numCount; ++ I) {int count = 0; // duplicate elements cannot be repeated. if (I> 0 & nums [I]. first = nums [I-1]. first) {continue;} // if int countVisit = visited. size (); // count the number of times that repeated elements have accessed for (int j = 0; j <countVisit; ++ j) {if (visited [j] = nums [I]. first) {count ++;} // if} // for // if any element has not accessed if (count <nums [I]. second) {visited. push_back (nums [I]. first); DFS (nums, visited, result); visited. pop_back () ;}// if} // for }}; int main () {Solution solution; vector <int> num; num. push_back (1); num. push_back (2); num. push_back (1); // rearrange the vector <int> permutes = solution. permuteUnique (num); // output for (int I = 0; I <permutes. size (); I ++) {cout <"["; for (int j = 0; j <permutes [I]. size (); j ++) {cout <permutes [I] [j];} // for cout <"]" <endl;} return 0 ;}