forked from jyx-fyh/algorithm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstrAllPermutation.cpp
More file actions
62 lines (58 loc) · 1.24 KB
/
strAllPermutation.cpp
File metadata and controls
62 lines (58 loc) · 1.24 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
//
// Created by ButcherX on 23-11-17.
//
#include<iostream>
#include<string>
#include<unordered_set>
using std::string;
using std::unordered_set;
//生成式
void process1(string str, int cur, string path, unordered_set<char> set)
{
if(cur == str.size())
{
std::cout << path << std::endl;
return;
}
for(int i = 0; i < str.size(); i++)
{
if(set.find(str[i]) == set.end())
{
set.emplace(str[i]);
process1(str, cur + 1, path + str[i], set);
set.erase(str[i]);//恢复现场,容易忽略
}
}
}
void printAllPermutation1(string str)
{
unordered_set<char> set;
string path = "";
process1(str, 0, path, set);
}
//交换式-参数设计更好
void process2(string str, int index)
{
if(index == str.size())
{
std::cout << str << std::endl;
return;
}
for(int i = index; i < str.size(); i++)
{
std::swap(str[index], str[i]);
process2(str, index + 1);
std::swap(str[index], str[i]);
}
}
void printAllpremutation2(string str)
{
process2(str, 0);
}
int main()
{
string str = "1234";
printAllPermutation1(str);
std::cout << "==========" << std::endl;
printAllPermutation1(str);
}