[LeetCode] [Java] Permutation Sequence

Source: Internet
Author: User

[LeetCode] [Java] Permutation Sequence
Question:

 

The set[1,2,3,…,n]Contains a total of n! Unique permutations.

By listing and labeling all of the permutations in order,
We get the following sequence (ie, for n = 3 ):

  1. 123
  2. 132
  3. 213
  4. 231
  5. 312
  6. 321

     

    Given n and k, return the kth permutation sequence.

    Note: Given n will be between 1 and 9 inclusive.

    Question:

    Set [1, 2, 3 ,..., N] contains a total of n! Different Combinations exist and are unique.

    List and mark all these combinations in order. We get the following sequence (when n = 3 ):

     

    1. 123
    2. 132
    3. 213
    4. 231
    5. 312
    6. 321Given n and k, the k Elements in the sequence are returned. N is a number between 1 and 9.

       

      Algorithm analysis:

      Method 1:

      Using the method of the question permutations, we first get all the arrays and then find the k elements, but it times out. The Code will also be provided later.

      Method 2:

      Reference blog http://www.cnblogs.com/springfor/p/3896201.html

       

      The mathematical solution is not suitable for this question. Let's take a look.

       

      The question tells us:N can be n! Sort; then there will be (n-1) n-1 numbers )! .

      For n-digit digits, if the highest bit is not included, the n-1 bit after it will have(N-1 )! .

      Therefore, for n-digits, each of the different highest digits can be spliced (n-1 )! .

      So you can think of it as following each group (n-1 )! Group.

      K/(n-1 )! You can obtain the index of the highest bit in the sequence. In this way, the highest bit in the k-th arrangement can be obtained from the index bit in the series, and this number must be deleted from the series.

      Then, the new k can have k % (n-1 )! . (Forgive me for being stupid. I don't want to know why I want to update k ~~)

      Loop n times. At the same time, in order to be able to match the array coordinates, k first --.


      AC code:

      Method 1: (timeout)

       

      Public String getPermutation (int n, int k) {int num [] = new int [n]; ArrayList> temres = new ArrayList> (); for (int I = 0; I
           
            
      > Permute (int [] num) {ArrayList> result = new ArrayList> (); permute (num, 0, result); return result ;} static void permute (int [] num, int start, ArrayList> result) {if (start> = num. length) // termination condition, recursively returning to the end node is to convert the array into a linked list {ArrayList
            
             
      Item = convertArrayToList (num); result. add (item) ;}for (int j = start; j <= num. length-1; j ++) {swap (num, start, j); // exchange permute (num, start + 1, result ); // recursive swap (num, start, j) of the child after switching; // restore to the initial state before switching to facilitate the next exchange result} private static ArrayList
             
              
      ConvertArrayToList (int [] num) // array variable linked list {ArrayList
              
               
      Item = new ArrayList
               
                
      (); For (int h = 0; h <num. length; h ++) item. add (num [h]); return item;} private static void swap (int [] a, int I, int j) // exchange {int temp = a [I]; a [I] = a [j]; a [j] = temp ;}
               
              
             
            
           

      Method 2:

       

       

      Public String getPermutation (int n, int k) {k --; // to transfer it as begin from 0 rather than 1 List
           
            
      NumList = new ArrayList
            
             
      (); For (int I = 1; I <= n; I ++) numList. add (I); int factorial = 1; for (int I = 2; I <n; I ++) factorial * = I; StringBuilder res = new StringBuilder (); // simple replacement of StringBuffer | used by a single thread in the string buffer | it is faster than StringBuffer (most of time) int times = n-1; while (times> = 0) {int indexInList = k/factorial; res. append (numList. get (indexInList); numList. remove (indexInList); k = k % factorial; // new k for next turn if (times! = 0) factorial = factorial/times; // new (n-1 )! Times --;} return res. toString ();}
            
           


       

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.