###### leetcode 60: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.

class Solution {
public:
string getPermutation(int n, int k) {
vector<char> nums;
for (int i = 1; i <= n; i++) nums.push_back(i+'0');
count = 0;
string result;
back(0, n, k, nums, result);
return result;
//return result.substr(0,n);
}
void back(int index, int size, int k, vector<char>& nums,string &result)
{
if (index >= size)
{
count++;
if (count >= k)	result.assign(nums.begin(),nums.end());
return;
}
for (int i = index; i<size && count<k; i++)
{
swap(nums[i], nums[index]);
back(index + 1, size, k, nums,result);
swap(nums[i], nums[index]);
}
}
private:
int count;
};

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

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

2016-03-04 09:07:30

#### LeetCode(60): Permutation Sequence (C++)

2015-12-01 10:44:16

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

2017-08-01 16:23:00

#### (Java)LeetCode-60. Permutation Sequence

2016-10-05 23:52:58

#### 【leetcode】【60】Permutation Sequence

2016-03-02 09:21:16

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

2016-04-30 23:06:32

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

2016-05-21 09:05:38

#### Leetcode: Permutation Sequence

2013-10-17 17:56:05

#### [C++]LeetCode: 114 Permutation Sequence（返回第k个阶乘序列——寻找数学规律）

2015-01-20 20:48:30

#### Permutation Sequence -- LeetCode

2014-03-25 06:41:17

## 不良信息举报

leetcode 60:Permutation Sequence