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


 

在面试时需要注意咨询面试官,输入的k 是否小于1 或者 是否大于n!

分析:按照一般的递归求全排列的算法(LeetCode:Permutations),输出的序列不是按字典序有序的,比如对于1,2,3,输出序列为:

1 2 3
1 3 2
2 1 3
2 3 1
3 2 1
3 1 2

以3开头的排列举例,算法中是把1和3交换得到3 2 1,然后递归的求解,但是3 2 1不是以3开头的最小序列,应该是3 1 2. 为了得到有序的序列,我们不是把1 3 交换,而是应该把3移动到1的前面,这样得到的第一个以3开头的序列就是3 1 2。因此有如下的算法:

算法1

  View Code

该算法在大数据下超时了。


 

算法2

利用next_permutation函数(该函数的详解请参考LeetCode:Permutations算法3),这种做法也超时了

  View Code

算法3                                                 

上面的算法都是逐个的求排列,有没有什么方法不是逐个求,而是直接构造出第k个排列呢?我们以n = 4,k = 17为例,数组src = [1,2,3,...,n]。

第17个排列的第一个数是什么呢:我们知道以某个数固定开头的排列个数 = (n-1)! = 3! = 6, 即以1和2开头的排列总共6*2 = 12个,12 < 17, 因此第17个排列的第一个数不可能是1或者2,6*3 > 17, 因此第17个排列的第一个数是3。即第17个排列的第一个数是原数组(原数组递增有序)的第m = upper(17/6) = 3(upper表示向上取整)个数。                                           本文地址

第一个数固定后,我们从src数组中删除该数,那么就相当于在当前src的基础上求第k - (m-1)*(n-1)! = 17 - 2*6 = 5个排列,因此可以递归的求解该问题。

复制代码
 1 class Solution {
 2 public:
 3     string getPermutation(int n, int k) {
 4         string str = string("123456789").substr(0, n);
 5         string res(n, ' ');
 6         for(int i = 0; i < n; i++)
 7             res[i] = helper(str, k);
 8         return res;
 9     }
10     //以s中字符构造的全排列中,返回第k个排列的第一个字符,并且删除s中该字符
11     //s中字符递增有序
12     char helper(string &s, int &k)
13     {
14         int tmp = factorial(s.size()-1), i = (k-1)/tmp;
15         char res = s[i];
16         s.erase(i, 1);
17         k -= i*tmp;//更新k
18         return res;
19     }
20     //求正整数n的阶乘
21     int factorial(int n)
22     {
23         int res = 1;
24         for(int i = 2; i <= n; i++)
25             res *= i;
26         return res;
27     }
28 };
复制代码

当然也可以非递归实现

复制代码
 1 class Solution {
 2 public:
 3     string getPermutation(int n, int k) {
 4         int total = factorial(n);
 5         string candidate = string("123456789").substr(0, n);
 6         string res(n,' ');
 7         for(int i = 0; i < n; i++)//依次计算排列的每个位
 8         {
 9             total /= (n-i);
10             int index = (k-1) / total;
11             res[i] = candidate[index];
12             candidate.erase(index, 1);
13             k -= index*total;
14         }
15         return res;
16     }
17     int factorial(int n)
18     {
19         int res = 1;
20         for(int i = 2; i <= n; i++)
21             res *= i;
22         return res;
23     }
24 };
复制代码

 






本文转自tenos博客园博客,原文链接:http://www.cnblogs.com/TenosDoIt/p/3721918.html,如需转载请自行联系原作者

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值