-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path60.cpp
More file actions
executable file
·44 lines (44 loc) · 975 Bytes
/
60.cpp
File metadata and controls
executable file
·44 lines (44 loc) · 975 Bytes
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
#include <string>
#include <algorithm>
#include <iterator>
using namespace std;
class Solution {
public:
int num;
int cur[100];
bool flag[100];
int stopk;
int pc;
bool stopflag;
void dfs(int index){
if (index == num){
pc++;
if (pc == stopk){
stopflag = true;
return;
}
}
for (int i = 1; i <=num;i++){
if (flag[i] == false){
cur[index] = i;
flag[i] = true;
dfs(index+1);
flag[i] = false;
if (stopflag)
return;
}
}
}
string getPermutation(int n, int k) {
stopflag = false;
pc = 0;
stopk = k;
memset(flag, 0, sizeof(flag));
num = n;
string answer = "";
for (int i = 0; i < k;i++){
answer += char('0'+cur[i]);
}
return answer;
}
};