leetcode Permutation Sequence

原创 2013年12月03日 20:26:05

Permutation Sequence

 Total Accepted: 1819 Total Submissions: 9060My Submissions

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.

Discuss



Three problems:

1. overly dependent on next_permutation, TLE

2. --k. If k%a! == b, so the bth character should be brought forward from the rear a elements.

3. If the length is n, n - 1 adjustments are enough.  So i = n instead of n - 1

for (i = n - 1; i >= 1; --i) {

class Solution {
 public:
  int factorial[10];
  string getPermutation(int n, int k) {
    string res = "";
    int i, j, tmp;
    vector<int> pos;
    factorial[0] = 1;
    for (i = 1; i <= n; ++i) {
      factorial[i] = factorial[i - 1] * i;
      res.push_back('0' + i);
    }
    --k;
    for (i = n - 1; i >= 1; --i) {
      pos.push_back(k / factorial[i]);
      k = k % factorial[i];
    }
    for (i = 0; i < pos.size(); ++i) {
      tmp = res[i + pos[i]];
      for (j = pos[i]; j >= 1; --j)
        res[i + j] = res[i + j - 1];
      res[i] = tmp;
    }
    
    return res;
  }
};

Give a WA code here, find the bug:

class Solution {
 public:
  string getPermutation(int n, int k) {
    vector<int> factorial(n, 1);
    string res(n, '1');
    for (int i = 1; i < n; ++i) {
      factorial[i] = factorial[i-1]*i;
      res[i] += i - 1;
    }
    --k;
    for (int i = 0; i < n; ++i) {
      int tmp = k / factorial[n - 1 - i];
      char ch = res[i + tmp];
      for (int j = tmp + i; j > i; --j)
        res[j] = res[j - 1];
      res[i] = ch;
      k = k % factorial[n - 1 - i];
    }
    return res;
  }
};

Another AC version:

class Solution {
 public:
  string getPermutation(int n, int k) {
    vector<int> factorial(n, 1);
    string res(n, '1');
    for (int i = 1; i < n; ++i) {
      factorial[i] = factorial[i-1]*i;
      res[i] += i;
    }
    --k;
    for (int i = 0; i < n; ++i) {
      int tmp = k / factorial[n - 1 - i];
      char ch = res[i + tmp];
      for (int j = tmp + i; j > i; --j)
        res[j] = res[j - 1];
      res[i] = ch;
      k = k % factorial[n - 1 - i];
    }
    return res;
  }
};



相关文章推荐

[Leetcode] 60. Permutation Sequence 解题报告

题目: The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all ...

LeetCode 60. Permutation Sequence

1. 题目要求 The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeli...

[LeetCode]60. Permutation Sequence

60. Permutation Sequence The set [1,2,3,…,n] contains a total of n! unique permutations. By listin...
  • Johnkui
  • Johnkui
  • 2016年04月15日 00:21
  • 229

Permutation Sequence--LeetCode

题目: The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all ...

LEETCODE60. Permutation Sequence

60. Permutation Sequence 内容如下: 已知正整数n的全排列按照字典序: 1 123 2 132 3 213 4 231 5 312 6 321 先给出n和k...

leetcode[60]Permutation Sequence 以及 全排列的编码与解码——康托展开 (附完整代码)

leetcode[60]Permutation Sequence 以及 全排列的编码与解码——康托展开 (附完整代码)...

[Leetcode] 60. Permutation Sequence

题目: The set [1,2,3,…,n] contains a total of n! unique permutations.By listing and labeling all of t...

LeetCode: Permutation Sequence [059]

【题目】 The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all ...

[LeetCode]Permutation Sequence

Question The set [1,2,3,…,n] contains a total of n! unique permutations.By listing and labeling all...
  • byamao1
  • byamao1
  • 2016年10月26日 17:19
  • 103

Leetcode刷题记—— 60. Permutation Sequence(排列序列)

一、题目叙述: The set [1,2,3,…,n] contains a total of n! unique permutati...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:leetcode Permutation Sequence
举报原因:
原因补充:

(最多只允许输入30个字)