Leetcode 60. Permutation Sequence

Source: Internet
Author: User

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 would be between 1 and 9 inclusive.

"Problem description"

Given the n numbers of 1 to N, there are n! in their permutation combinations. , the number of these numbers is sorted in ascending order, and the K is returned.

Ideas

Let's take a look at the above example, and for [All-in-one], here are their combinations:

These combinations are sorted by size and we find that they start with 1/2/3, and the number of permutations that begin with these numbers is the same. In an arrangement beginning with 1, if you do not look at the highest level, then the remainder begins with 2/3. And the number of permutations that begin with them is the same. After discovering this rule, we can do something like this:

First maintain a list of <1,2,..., N>, and the elements in the list are sorted in ascending order.

Given an n and K, we can calculate the total number of permutations is n!, then K is the first number of the beginning of it? This number is (k-1)/(n-1)! + 1, why is K minus 1? such as k=2, then K/2 + 1 = 2, this will cause if K can be (n-1)! The calculated start number increases by 1, and the K minus 1 calculates the correct starting number.

After calculating the starting number is the number of M, we want to calculate in the number of numbers beginning with the number of M, we want to find the first number, then with the new k = (k-1)% (n-1)! + 1. The k minus 1 here is also to prevent K-divisible (n-1)! factorial, K (n-1)! becomes 0, and at this time k should be equal to (n-1)!.

After we find the start number, we remove M from the original number sequence and proceed to the above lookup process. Until all the elements in the list are removed.

As an example:n = 3, k = 4:

1. Elements in the list <1,2,3>

2. Find the start number: m= (k-1)/(n-1)! + 1 = 2. The second element is the starting number, and the second element is 2.

3. In the arrangement starting with the second element we find the K = (k-1)% (n-1)! + 1 = 2.

4. Delete the second element and the list becomes <1,3>,n=2,k=2.

5. m = 2, the second element in the list is 3.

6. K = 1.

7. The list becomes <1>,n=1,k=1.

8. The last digit is 1.

9. Return the result 231.

What do you think? Do you understand the process?

"Java Code"

1  Public classSolution {2      PublicString Getpermutation (intNintk) {3list<integer> list =NewLinkedlist<integer>();4         intTotalnum = 1;5StringBuilder SB =NewStringBuilder ();6          for(inti = 1; I <= N; i++) {7 List.add (i);8Totalnum *=i;9         }Ten  One          for(inti = n; I >= 1; i--) { ATotalnum/=i; -             intRank = (k-1)/Totalnum; -K = (k-1)% totalnum + 1; the Sb.append (List.get (rank)); - List.remove (rank); -         } -  +         returnsb.tostring (); -     } +}

Leetcode 60. Permutation Sequence

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.