JasonChang

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

找出规律

 1 public class Solution {
 2     public String getPermutation(int n, int k) {
 3         int total = 1;
 4         ArrayList<Integer> rest = new ArrayList<Integer>();
 5         StringBuilder result = new StringBuilder();
 6         int m = n;
 7         
 8         while(m > 1){
 9             total*=m;
10             m--;
11         }
12         
13         for(int i = 1; i <= n; i++)
14             rest.add(i);
15             
16         k--;
17         while(!rest.isEmpty()){
18             total /= n;
19             n--;
20             int index = k / total;
21             result.append(String.valueOf(rest.get(index)));
22             k %= total;
23             rest.remove(index);
24         }
25         return result.toString();
26     }
27 }

 

 

posted on 2013-11-07 12:24  JasonChang  阅读(177)  评论(0编辑  收藏  举报