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 ();}