[C + +] leetcode:120 permutations II

Source: Internet
Author: User
Tags repetition

Topic:

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] .

Ideas: This problem and The difference between permutations is that the input array of numbers contains duplicate elements. If we do not deal with duplicate elements, we will produce duplicate results. So how do you avoid this repetition? Following our approach to dealing with such problems, we first sort the input arrays, then skip the recursive function calls for repeating element loops, recursive only the first unused element, and this time the result will appear in the result of the first recursive function, since the repeating element will repeat this recursive result. The subsequent results are skipped. If the element in front of the first repeating element is not in the previous result (!used[i-1]), then we do not need recursion. For example [1,1,1,2], if the first 1 in the result (used[0] = true), we next loop, we determine to the second 1, only recursive and push the TMP, otherwise it will result in duplication. This is important and needs to be considered. In general, we need to judge the repetition of elements and the use of the preceding elements.

AC Code:

Class Solution {public:vector<vector<int> > Permuteunique (vector<int> &num) {vector<v        ector<int> > ret;                if (num.size () = = 0) return ret;        vector<int> tmp;        Vector<bool> Used (Num.size (), false);        Sort (Num.begin (), Num.end ());        Permuteunique_helper (NUM, tmp, used, ret);    return ret; }private:void permuteunique_helper (vector<int>& num, vector<int> tmp, vector<bool> used, vector            <vector<int> >& ret) {if (tmp.size () = = Num.size ()) {ret.push_back (TMP);        Return } for (int i = 0; i < num.size (); i++) {if (i > 0 &&!used[i-1] && n                        Um[i-1] = = Num[i]) continue;                if (!used[i]) {tmp.push_back (num[i]);                Used[i] = true;                Permuteunique_helper (NUM, tmp, used, ret); Tmp.pop_back ();            Used[i] = false;    }} return; }};
This solution with a general, put in the permutations is also correct, so in the interview, we can directly implement this code, do not assume that the element is not duplicated, can be discussed with the interviewer, but generally we have to consider repeating elements.


This problem, we can also use the iterative method to do, you can use next permutation algorithm, to ensure the order, from the smallest arrangement to the largest permutation iteration down, the code is relatively lengthy, the general interview will not test the method of the iteration, but you can see this blog post:

permutations, permutations II (perfection arrangement)




[c++]leetcode:120 permutations II

Related Article

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.