forked from timoncui/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPermutation_Sequence.cpp
More file actions
61 lines (48 loc) · 1.11 KB
/
Permutation_Sequence.cpp
File metadata and controls
61 lines (48 loc) · 1.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/*
Author: Timon Cui, [email protected]
Title: Permutation Sequence
Description:
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):
"123"
"132"
"213"
"231"
"312"
"321"
Given n and k, return the kth permutation sequence.
Note: Given n will be between 1 and 9 inclusive.
Difficulty rating:
Source:
http://www.leetcode.com/onlinejudge
Notes:
http://en.wikipedia.org/wiki/Lehmer_code
O(n^2)
*/
#include "utils.hpp"
using namespace std;
class Solution {
public:
string getPermutation(int n, int k) {
k --;
vector<int> v(n);
for (int i = 1; i <= n; ++i) {
v[n - i] = k % i;
k /= i;
}
vector<char> c(n);
for (int i = 0; i < n; ++i) c[i] = i + '1';
string s(n, '0');
for (int i = 0; i < n; ++i) {
s[i] = c[v[i]];
c.erase(c.begin() + v[i]);
}
return s;
}
};
int main() {
Solution s;
eq(0, s.getPermutation(4, 7), "2134");
eq(1, s.getPermutation(9, 155915), "489523716");
}