2015 Meituan network Harbin Institute of Technology K

Source: Internet
Author: User

 

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

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.