-
Notifications
You must be signed in to change notification settings - Fork 0
/
permutation_sequence.cpp
54 lines (48 loc) · 1.13 KB
/
permutation_sequence.cpp
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
// https://leetcode.com/problems/permutation-sequence/
// June 01, 2016
#include <iostream>
#include <string>
#include <vector>
#include <sstream>
using namespace std;
class Solution {
public:
string getPermutation(int n, int k)
{
string output_str = "";
vector<string> vec; // "1",..,"n"
stringstream ss;
for (int i = 1; i != (n+1); ++i)
{
ss << i;
vec.push_back(ss.str());
ss.str(string());
}
int num = k-1;
int denom = 1;
// denom = (n-1)!
for (int i = 1; i != n; ++i)
{
denom *= i;
}
for (int i = 1; i != n; ++i)
{
int index = num/denom;
num = num%denom;
output_str += vec[index];
vec.erase(vec.begin()+index);
denom /= (n-i);
}
output_str += vec[0];
return output_str;
}
};
int main(int argc, char* argv[])
{
int n = 3;
int k = 3;
Solution sln;
string permutation_at_position_k = sln.getPermutation(n,k);
cout << "permutation at position k: " << permutation_at_position_k << endl;
return 0;
}