两种排列对比

可重复

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
#include<iostream>
#include<vector>
#include<string>

using namespace std;
vector<char> record = {'a', 'b', 'c'};
vector<vector<char>> result;

void Permutation(vector<char> &current)
{
if (current.size() == record.size())
{
result.push_back(current);
return;
}
for (unsigned int i = 0; i < record.size(); i++)
{
current.push_back(record[i]);
Permutation(current);
current.pop_back();
}
}

int main()
{
vector<char> current;
Permutation(current);
return 0;
}

这里的index表示的是当前current中数据的个数

不可重复

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
#include<iostream>
#include<vector>
#include<string>

using namespace std;
vector<char> record = {'a', 'b', 'c'};
vector<vector<char>> result;

void Permutation2(vector<char> &current, int index)
{
if (index == record.size())
{
result.push_back(current);
return;
}
for (unsigned int i = index; i < record.size(); i++)
{
swap(record[i], record[index]);
Permutation2(record, index + 1);
swap(record[i], record[index]);
}
}

int main()
{
Permutation2(record, 0);
return 0;
}

这里的index表示index之前的保持不变,交换index和他之后的字符