[leetcode]Permutation Sequence

來源:互聯網
上載者:User

標籤:style   blog   http   color   os   strong   

Permutation Sequence

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

演算法思路:

思路1:

最直觀的思路,dfs逐個求,並計數。提交之後逾時。

代碼如下:

 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

 

思路2:

跳過中間那些無用的,直接求第k個。

尋找規律:

以n = 4,k = 20為例。以1為頭的字串一共有3!= 6個,同理以2、3開頭的也有6個,因此第20個必定以4開頭。

接下來只需要求以4開頭的第2(20 - 18)個即可。

再遞迴處理。

 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 }

 

 

思路3:

迭代處理

傳遞門

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.