-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPermutation_Sequence.cpp
More file actions
72 lines (61 loc) · 1.47 KB
/
Permutation_Sequence.cpp
File metadata and controls
72 lines (61 loc) · 1.47 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
// Source : https://oj.leetcode.com/problems/permutation-sequence/
// Author : zheng yi xiong
// Date : 2015-02-09
/**********************************************************************************
*
* 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 k th permutation sequence.
* Note: Given n will be between 1 and 9 inclusive.
*
**********************************************************************************/
#include "stdafx.h"
#include <iostream>
#include <vector>
#include <string>
using namespace std;
class Solution {
public:
string getPermutation(int n, int k) {
vector<char> num(n);
int sumSequence = 1;
for (int i = 0; i < n; ++i)
{
num[i] = '0' + i + 1;
sumSequence *= (i + 1);
}
if (k > sumSequence)
{
return "";
}
string s(n, '0');
int s_pos = 0;
sumSequence /= (n - s_pos);
int cur_pos = (k - 1) / sumSequence;
k = (k - 1) % sumSequence;
s[s_pos] = num[cur_pos];
num.erase(num.begin() + cur_pos);
for (s_pos = 1; s_pos < n; ++s_pos)
{
sumSequence /= (n - s_pos);
cur_pos = k / sumSequence;
k = k% sumSequence;
s[s_pos] = num[cur_pos];
num.erase(num.begin() + cur_pos);
}
return s;
}
};
int _tmain(int argc, _TCHAR* argv[])
{
Solution so;
string str = so.getPermutation(3, 4);
return 0;
}