Question: Like the previous question, the given number may be repeated at this time.
Practice: you only need to sort the order of num and then judge that if the current value is equal to the previous one, then skip and there will be no repeated persons. It is AC.
1 class solution {2 public: 3 vector <int> permuteunique (vector <int> & num) 4 {5 vector <int> ans; 6 if (Num. size () = 1) 7 {ans. push_back (Num); Return ans;} 8 9 vector <int> post; 10 vector <int> TMP; 11 vector <int> cur; 12 sort (Num. begin (), num. end (); // sort the 13 for (INT I = 0; I <num. size (); ++ I) 14 {15 TMP = num; 16 if (I-1> = 0 & TMP [I] = TMP [I-1]) continue; // skip 17 TMP if the number is repeated. erase (TMP. begin () + I); 18 Post = permuteunique (TMP); 19 for (Int J = 0; j <post. size (); ++ J) 20 {21 cur = post [J]; 22 cur. insert (cur. begin (), num [I]); 23 ans. push_back (cur); 24} 25} 26 return ans; 27} 28 };
This person is also recursive. There are several good examples of this person. Take a good look after you have time.
Leetcode permutations II