The permutation sequence on leetcode is the executable code below
1 2 3
1 3 2
2 1 3
2 3 1
3 1 2
3 2 1
123,132 starting with 1, a total of 2! Number
2 beginning with 213,231
3 starting with 312,321
If you give your brother K, you can find out who it starts? Just find the number of 2! This is the overall idea.
2 3 Import Java. util. arraylist; 4 5 public class main {6 // calculate the factorial of N 7 public static int FIC (int n) 8 {9 int res = 1; 10 for (INT I = 1; I <= N; I ++) 11 {12 res = res * I; 13} 14 return res; 15} 16 17 18 public static string getpermutation (INT N, int K) {19 int r = FIC (n); 20 if (k> r) return NULL; 21 arraylist <integer> arr = new arraylist <integer> (); 22 For (INT I = 1; I <= N; I ++) 23 {24 arr. add (I); 25} 26 27 // form the list of 1, 2, 3, 4, 5, 7 28 // 29 30 31 K --; 32 33 stringbuffer SBF = new stringbuffer (); 34 For (INT I = N; I> = 1; I --) // because there are n arrays, we constantly delete one from the linked list and add it to the string, the last one is directly added to the End 35 {36 r = r/I; 37 int T = K/R; // select the delete location 38 SBF. append (ARR. remove (t); 39 K = K % R; 40 41 42 43 44} 45 46 Return SBF. tostring (); 47 48} 49 public static void main (string ARGs []) 50 {51 52 system. out. println (getpermutation (2, 2); 53 54 55 56} 57}
2015 Meituan network Harbin Institute of Technology K