LeetCode Online Judge 题目C# 练习 - 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):
"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.

 1         public static string PermutationSequence(int n, int k)
 2         {
 3             List<int> num = new List<int>();
 4             for (int i = 1; i <= n; i++)
 5             {
 6                 num.Add(i);
 7             }
 8 
 9             for (int i = 1; i < k; i++)
10             {
11                 NextPermutation(num);
12             }
13 
14             string ret = "";
15             foreach (var item in num)
16             {
17                 ret += Convert.ToString(item);
18             }
19 
20             return ret;
21         }

代码分析:

  如果做了之前的一题, Next Permutation,这一题就是吃菜了。

posted @ 2012-10-05 04:39  ETCOW  阅读(262)  评论(0编辑  收藏  举报