[Leetcode] Permutations II

Source: Internet
Author: User

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

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.