leetcode_37 Permutation Sequence

161 篇文章 0 订阅

题目:排列次序

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 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.
  • Given k will be between 1 and n! inclusive.

方法一:使用回溯法 (开始用 /*  */ 里面的方法,不过第一个if里面没有--cnt,结果是错误的,回溯的时候,还需要用到,不是直接返回的),TLE

class Solution {
public:
    unordered_map<int,int> cand;
    string res = "";
    int cnt;
    void backtrace(string perm, int n,int left)
    {
        if(left == 0 || cnt == 0)
        {
            if(--cnt == 0)
            res = perm;
            return;
        }
        /*if(left==0 && cnt == 1)
        {
            --cnt;
            res = perm;
            return;
        }
        if(left == 0 || cnt == 0)
        {
            if(cnt) cnt--;
            return;
        }*/
        for(int i = 1; i <= n; i++)
        {
           if(cand[i] > 0){ 
            cand[i]--;   
            backtrace(perm + to_string(i),n,left-1);
            cand[i]++;
           }
        }
    }
    string getPermutation(int n, int k) {
        string perm ="";
        for(int i = 1; i <= n; i++)
        {
            cand[i]++;
        }
        cnt = k;
        backtrace(perm,n,n); 
        return res;
    }
};

方法二:根据排列组合的规律来生成,第Kth个序列

I'm sure somewhere can be simplified so it'd be nice if anyone can let me know. The pattern was that:

say n = 4, you have {1, 2, 3, 4}

If you were to list out all the permutations you have

1 + (permutations of 2, 3, 4)
2 + (permutations of 1, 3, 4)
3 + (permutations of 1, 2, 4)
4 + (permutations of 1, 2, 3)

We know how to calculate the number of permutations of n numbers... n! So each of those with permutations of 3 numbers means there are 6 possible permutations. Meaning there would be a total of 24 permutations in this particular one. So if you were to look for the (k = 14) 14th permutation, it would be in the

3 + (permutations of 1, 2, 4) subset.

To programmatically get that, you take k = 13 (subtract 1 because of things always starting at 0) and divide that by the 6 we got from the factorial, which would give you the index of the number you want. In the array {1, 2, 3, 4}, k/(n-1)! = 13/(4-1)! = 13/3! = 13/6 = 2. The array {1, 2, 3, 4} has a value of 3 at index 2. So the first number is a 3.

Then the problem repeats with less numbers.

The permutations of {1, 2, 4} would be:

1 + (permutations of 2, 4)
2 + (permutations of 1, 4)
4 + (permutations of 1, 2)

But our k is no longer the 14th, because in the previous step, we've already eliminated the 12 4-number permutations starting with 1 and 2. So you subtract 12 from k.. which gives you 1. Programmatically that would be...

k = k - (index from previous) * (n-1)! = k - 2*(n-1)! = 13 - 2*(3)! = 1

In this second step, permutations of 2 numbers has only 2 possibilities, meaning each of the three permutations listed above a has two possibilities, giving a total of 6. We're looking for the first one, so that would be in the 1 + (permutations of 2, 4) subset.

Meaning: index to get number from is k / (n - 2)! = 1 / (4-2)! = 1 / 2! = 0.. from {1, 2, 4}, index 0 is 1
so the numbers we have so far is 3, 1... and then repeating without explanations.
{2, 4}
k = k - (index from pervious) * (n-2)! = k - 0 * (n - 2)! = 1 - 0 = 1;
third number's index = k / (n - 3)! = 1 / (4-3)! = 1/ 1! = 1... from {2, 4}, index 1 has 4
Third number is 4

{2}
k = k - (index from pervious) * (n - 3)! = k - 1 * (4 - 3)! = 1 - 1 = 0;
third number's index = k / (n - 4)! = 0 / (4-4)! = 0/ 1 = 0... from {2}, index 0 has 2
Fourth number is 2

四个数字存在数组num中,index表示索引

num[] = {1,2,3,4}

用 f[n]来表示n的阶乘, f[n] = n! 

f[0] = 1, f[1] = 1 ; f[2] = 2; f[3] = 6; f[4] = 24

k = 14;  k = k - 1 = 13; 从0开始

{1,2,3,4}       n - 1 = 3

k / f[3] = 2     index = 2 ------->第一个数字  3       k = k  - index * f[3]  = 1;

{1,2,4}          n - 2 = 2

k / f[2] = 0     index = 0 ------->第二个数字  1      k = k - index * f[2] = 1;

{2,4}            n - 3 = 1

k/f[1] = 1       index = 1------->第三个数字   4       k = k - index * f[1] = 0;

{2}            n - 4 = 0

k/f[0] = 0      index = 0 -------->第四个数字  2     

因此k = 14,数字为  3 1 4 2

代码如下:

class Solution {
public:
    string getPermutation(int n, int k) {
        int factor[n];
        factor[0] = 1;
        for(int i = 1;i < n; i++)
        {
            factor[i] = factor[i-1] * i;
        }
        vector<int> nums;
        for(int i = 1; i <= n; i++)
        {
            nums.push_back(i);
        }
        
        string res = "";
        int kth = k -1;
        for(int i = 1; i <= n; i++)
        {
            int index = kth / factor[n - i] ;
            res += to_string(nums[index]);
            nums.erase(nums.begin()+index);
            kth = kth - index*factor[n - i];
        }
        return res;
    }
};

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值