[leetcode]46 permutations

Source: Internet
Author: User

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

Http://fisherlei.blogspot.com/2012/12/leetcode-permutations.html


Todo

See subset combination, next

Public class solution {        // assumes all  input numbers are unique        public list <list<integer>> permute (Int[] num)  {         return permute_recursively (num);    }         ////////////////////    // Solution 2: Swap Recursively:     //    // swap 0-0, 0-1, 0-2, 0-3, ..,  0-k    // swap 1-1, 1-2, ... 1-k    //  swap 2-2 ...    // ...    // swap k-k     public list<list<integer>> permute_recursively (Int[] num)     {&nBsp;       list<list<integer>> result = new  arraylist ();         perm (0, num, result);         return result;    }         private void perm (int start, int[] num, list<list< Integer>> result)     {        int  Len = num.length;        if (Start >= len)          {        List<Integer>  tmp = new ArrayList<> (num.length);         for (Int n : num)             tmp.add (n);    &Nbsp;    result.add (TMP);        return;         }                 for (int i = start ; i < len ; i  ++)         {             swap (num, start, i);         perm ( Start + 1, num , result);         swap (num,  start, i);        }    }         private void swap (int[] a, int i, int j )     {        int tmp = A[i];     a[i] = A[j];    A[j] = tmp;    }             ////////////////////    // solution  1: Insertion:    //    // n = 1     //      1    //    // n  = 2    //      2,1    //       1,2    //    // n =  3    //      3,2,1    //       2,3,1    //      2,1,3     //      3,1,2    //       1,3,2    //      1,2,3    //    //  Known  n-1  set, for each element, at all possible to insert a number n.        //    // Bad point:     // if the input numbers contains dups, need to use set <Set<Integer>> to avoid dups.    public List<List< Integer>> permute_insertion (Int[] num)  {                 list<list<integer>> toreturn = new  ArrayList<> (;        if ) (num == null  ||  num.length == 0)             return  toreturn;                    // insert result when length =  1        toreturn.add (Collections.singletonlist (num[0));         for  (int i = 1 ; i <  num.length ; i ++)         {             List<List<Integer>> newresults =  New arraylist<> ();            for  (List<integer> lastr : toreturn)              {                 for  (Int j = 0 ; j <= lastr.size ()  ; j++)                  {                     list<integer> newr =  new ArrayList<> (LASTR);                     newr.add (J, num[i]);                     newresults.add (NEWR) ;                }             }             toreturn = newresults;        }         return toreturn;    }}


[leetcode]46 permutations

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.