[leetcode]47 permutations II

Source: Internet
Author: User

https://oj.leetcode.com/problems/permutations-ii/

http://blog.csdn.net/linhuanmars/article/details/21570835


Public class solution {    public list<list<integer>>  permuteunique (Int[] num)      {         // Solution A:        // return  Permuteunique_swap (num);                 // solution b:        return permuteunique_ NP (num);     }    ////////////////////////    //  Solution B: NP    //    private List<List< INTEGER&GT;&GT;&NBSP;PERMUTEUNIQUE_NP (Int[] num)     {         arrays.sort (num);         boolean[] used =  new boolean[num.length];&nbSp;       list<list<integer>> results = new  ArrayList<> (        permuteunique_nphelp); num, used,  new ArrayList<Integer> (),  results);         Return results;    }        private void  permuteunique_nphelp (Int[] num, boolean[] used, list<integer> items,  list<list<integer>> results)     {         if  (Items.size ()  == num.length)          {            results.add (new ArrayList <Integer> (items));            return;          }                for   (int i = 0 ; i < num.length ; i ++)          {            / / if num[i] equals to some value been used before             // continue             // if used[i - 1] == true, that  is the first time num[i] occurs.             if  (i > 0 && !used[i - 1]  &AMP;&AMP;&NBSP;NUM[I]&NBSP;==&NBSP;NUM[I&NBSP;-&NBSP;1])                  continue;                         if  (Used[i])                  continue;                          Used[i] = true;            items.add (num [i]);                          permuteunique_nphelp (num, used, items, results);                          used[i] = false;             items.remOve (Items.size ()  - 1);         }    }             ////////////////////////     // Solution A: Recursive swap    //     private list<list<integer>> permuteunique_swap (Int[] num)       {        List<List<Integer>> result  = new arraylist<> ();         perm (num, 0,  Result);        return result;    }     private void perm (Int[] n, int start, list<list<integer >> result)     {        int len  = n.length;        if  (Start >= len)          {            // a result  found.            result.add (Listof (n));         }                 for  (Int i = start ; i < len  ; i ++)         {             // If we have any dups from start  To i.            // no need to  continue recursion            //              //  be careful not to assume that the following two practices can be re-:             //  (1) It is wrong to first sort the array at the beginning, and at each subsequent exchange, it is a mistake to ensure that the element that is currently being swapped is different from the previous element .             //  Although the order is started, the exchange of elements will make the array again unordered              //  (2) Each time you enter the recursive function permuterecur, sort the sub-array starting at the current index, This is also wrong .            //  because each time we exchange elements, we have to restore the elements of the interchange. , if you sort within a recursive function, the interchange element cannot be recovered correctly.                          if  (Unique (n, start, i))              {                 swap (N, i, start);          &Nbsp;      perm (N, start + 1, result);                 swap (N, i, start);             }         }    }        private boolean  unique (int[] n, int start, int end)     {         for  (int i = start ; i < end ;  i ++)         {             if  (N[i] == n[end])                  return false;         }        return true;    }         private list<integer> listof (int[] n)     {         List<Integer> toReturn = new ArrayList<> ( N.length);        for  (int i : n)              toreturn.add (i);         return toReturn;    }         private void swap (INT[]&NBSP;N,&NBSP;INT&NBSP;I&NBSP;,&NBSP;INT&NBSP;J)      {        int t = n[i];         n[i] = n[j];        n[j] =  t;    }}


[leetcode]47 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.