Leetcode: 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):"123""132""213""231""312""321"Given n and k, return the kth permutation sequence.Note: Given n will be between 1 and 9 inclusive.

When I saw this question, I immediately contacted next permutation. the question is to find the next larger permutation, and here is to find the largest permutation from small to large k, and we obviously know the smallest permutation, so we will use next permutation for subroutine, if you do this K times, do you find the required permutation. The time complexity of next permutation is O (n), where O (N * K ).

In the code, copy the int array into a char array to convert it to a string.

 1 public class Solution { 2     public String getPermutation(int n, int k) { 3         if (n < 0) return""; 4         int[] num = new int[n]; 5         char[] numcopy = new char[n]; 6         for (int z=0; z<n; z++) { 7             num[z] = z + 1; 8         } 9         for (int i=2; i<=k; i++) {10             nextPermutation(num);11         }12         for (int z=0; z<n; z++) {13             numcopy[z] = (char)(‘0‘ + num[z]);14         }15         return new String(numcopy);16     }17     18     public void nextPermutation(int[] num) {19         if (num == null || num.length == 0 || num.length == 1) return;20         int i = num.length-2;21         while (i>=0 && num[i]>=num[i+1]) {22             i--;23         }24         if (i >= 0) {25             int j = i;26             while (j<num.length-1 && num[j+1]>num[i]) {27                 j++;28             }29             int temp = num[i];30             num[i] = num[j];31             num[j] = temp;32         }33         reverse(num, i+1);34     }35     36     public void reverse(int[] num, int k) {37         int start = k;38         int end = num.length - 1;39         while (start < end) {40             int temp = num[start];41             num[start] = num[end];42             num[end] = temp;43             start++;44             end--;45         }46     }47 }

I am using this method directly, but the time complexity O (N * k) should be relatively large, because K can be set to n! Although OJ is passed, it is still not very good. Some of the practices on the Internet refer to it as a mathematical question for finding patterns. We know that the permutation of N numbers has n factorial in total. Based on this nature, we can find the number corresponding to a certain digit. The idea is like this. For example, if the current length is N, we know that each of the same starting elements corresponds to (n-1 )! Permutation (n-1 )! After permutation, a start element is changed. Therefore, as long as the current K is (n-1 )! The number obtained from the remainder is the index of the remaining array, so that the corresponding element can be obtained. This recursion ends until no element in the array ends. In implementation, we need to maintain an array to record the current element. Each time an element is obtained, it is added to the result array and then removed from the remaining array. Therefore, the space complexity is O (n ). A total of N rounds are required in time, and O (N ^ 2) is required for each element to be deleted if an array is used ). If you do not remove an element, you also need to mark it. Therefore, you must determine whether the first operation is a linear operation.

Code ganker practice (not further explored)

 1 public String getPermutation(int n, int k) { 2     if(n<=0) 3         return ""; 4     k--; 5     StringBuilder res = new StringBuilder(); 6     int factorial = 1; 7     ArrayList<Integer> nums = new ArrayList<Integer>(); 8     for(int i=2;i<n;i++) 9     {10         factorial *= i;11     }12     for(int i=1;i<=n;i++)13     {14         nums.add(i);15     }16     int round = n-1;17     while(round>=0)18     {19         int index = k/factorial;20         k %= factorial;21         res.append(nums.get(index));22         nums.remove(index);23         if(round>0)24             factorial /= round;25         round--;26     }27     return res.toString();28 }

 

leetcode: 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.