Leetcode permutations II

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.