# leetcode Permutation Sequence

### Permutation Sequence

Total Accepted: 1819 Total Submissions: 9060

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.

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: Permutation Sequence

The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of the ...
• doc_sgl
• 2013年10月17日 17:56
• 11730

## LeetCode 60 Permutation Sequence （Python实现及详解）

【题目】   The set [1,2,3,…,n] contains a total of n! uniquepermutations.   By listing and labeling ...
• yangjingjing9
• 2017年08月01日 16:23
• 220

## LeetCode-60-Permutation Sequence(找规律)-Medium

• eddy_liu
• 2016年03月04日 09:07
• 1377

## Permutation Sequence -- LeetCode

• linhuanmars
• 2014年03月25日 06:41
• 11249

## (Java)LeetCode-60. Permutation Sequence

The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of ...
• u012848330
• 2016年10月05日 23:52
• 295

## Leetcode #60. Permutation Sequence 排列组合序列 解题报告

1 解题思想这道题是要求第K个全排列组合的序列，什么意思呢，对于一个1…n的数字，我们一共会有n！个排列组合的序列，那么我们现在按照字典序为顺序，要求给出第K个序列。传统的做法呢，是使用字典序的方式进...
• MebiuW
• 2016年04月30日 23:06
• 904

## LeetCode 60. Permutation Sequence（排列序列）

• jmspan
• 2016年05月21日 09:05
• 316

## 【leetcode】【60】Permutation Sequence

• xiaoliucool1314
• 2016年03月02日 09:21
• 430

## LeetCode60:Permutation Sequence

The set [1,2,3,…,n] contains a total of n! unique permutations.By listing and labeling all of the pe...
• u012501459
• 2015年07月08日 10:45
• 620

## 60. Permutation Sequence（难）

The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of ...
• gao1440156051
• 2016年08月25日 21:30
• 849

举报原因： 您举报文章：leetcode Permutation Sequence 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)