[Leetcode] permutation sequence

Source: Internet
Author: User

Permutation sequence

The Set[1,2,3,…,n]Contains a totalN! Unique permutations.

By listing and labeling all of the permutations in order,
We get the following sequence (ie,N= 3 ):

  1. "123"
  2. "132"
  3. "213"
  4. "231"
  5. "312"
  6. "321"

 

GivenNAndK, ReturnKTh permutation sequence.

Note:GivenNWill be between 1 and 9 aggressive.

Algorithm ideas:

Thought 1:

The most intuitive idea is to find and count DFS one by one. Timeout after submission.

The Code is as follows:

 1 public class Solution { 2     boolean success = false; 3     StringBuilder str = new StringBuilder(); 4     int count = 0; 5     public String getPermutation(int n, int k) { 6         int count = 1; 7         for(int i = 1; i <= n; count*=i++); 8         if(k <= 0 || k > count ) return null; 9         boolean[] hash = new boolean[10];10         for(int i = 1; i <= n;hash[i++] = true);11         StringBuilder sb = new StringBuilder();12         dfs(sb,hash,k,n);13         return str.toString();14     }15     private void dfs(StringBuilder sb,boolean[] set,int k,int n){16         if(sb.length() == n){17             count++;18             if(count == k) {19                 success = true;20                 str = sb;21             }22             return;23         }24         for(int i = 1; i <= n ; i++){25             if(!set[i]) continue;26             set[i] = false;27             sb.append(i);28             dfs(sb, set, k, n);29             if(success) return;30             set[i] = true;31             sb.deleteCharAt(sb.length() - 1);32         }33     }34     35 }
View code

 

Idea 2:

Skip the useless ones in the middle and directly find the K.

Search rules:

Take n = 4, K = 20 as an example. There are 3 strings starting with 1! = 6, likewise, there are 6 at the beginning of 2 and 3, so 20th must start with 4.

Next, you only need to request 2nd (20-18) numbers starting with 4.

Recursive processing.

 1 public class Solution { 2     public String getPermutation(int n, int k) { 3         int count = 1; 4         for(int i = 1; i <= n; count*=i++); 5         if(k <= 0 || k > count ) return null; 6         List<Integer> list = new ArrayList<Integer>(); 7         for(int i = 1; i <= n; list.add(i++)); 8         StringBuilder sb = new StringBuilder(); 9         helper(n, k, list, sb);10         return sb.toString();11     }12     13     public void helper(int n,int k ,List<Integer> list,StringBuilder sb){14         if(n == 1) {15             sb.append(list.get(0));16             return;17         }18         int count = 1;19         for(int i = 1; i <= n - 1;count *= i++);20         int index = 0;21         while(k > count){22             index++;23             k -= count;24         }25         sb.append(list.get(index));26         list.remove(index);27         helper(n - 1,k,list,sb);28     }    29 }

 

 

Thought 3:

Iterative Processing

Transfer Gate

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.