forked from rost0413/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPermutation_Sequence.cpp
More file actions
78 lines (71 loc) · 1.6 KB
/
Permutation_Sequence.cpp
File metadata and controls
78 lines (71 loc) · 1.6 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
/*
Author: Weixian Zhou, ideazwx@gmail.com
Date: Jul 10, 2012
Problem: Permutation Sequence
Difficulty: medium
Source: http://www.leetcode.com/onlinejudge
Notes:
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.
Solution:
For set [1,2,3,...,n], there are a total of n! unique permutations.
Then we know there are n!/n=(n-1)! permutations start with 1, n!/n=(n-1)!
permutations start with 2, etc.
To get the kth permutation, We satrt by locating the digit from left
to right.
For n = 9, there are 362 880 unique permutations.
1 - 40320 starts with 1
40321 - 80640 starts with 2
80641 - 120960 starts with 3
...
*/
#include <vector>
#include <set>
#include <climits>
#include <algorithm>
#include <iostream>
#include <sstream>
#include <cmath>
#include <cstring>
#include <list>
using namespace std;
class Solution {
public:
int factorial(int n) {
if (n == 1 || n == 0) {
return 1;
}
return n * factorial(n - 1);
}
string getPermutation(int n, int k) {
list<int> num;
string result;
for (int i = 1; i <= n; i++) {
num.push_back(i);
}
for (int i = 0; i < n; i++) {
int f = factorial(n - i - 1);
int j = 0;
list<int>::iterator it = num.begin();
while (j * f < k) {
j++;
it++;
}
it--;
j--;
k -= j * f;
result.push_back(*it + '0');
num.erase(it);
}
return result;
}
};