#include<iostream>
using namespace std;
#include<vector>
//#include<string>
//#include<stack>
//#include<set>
//#include<unordered_set>
//#include<algorithm>
class Solution {
public:
void swapfunc(int& a, int& b) {
int temp = a;
a = b;
b = temp;
}
//递归定义:arr中index前的字符确定了,index及其index以后排列存在ans中
void GetAllSort(vector<int>arr, int index, vector<vector<int>>&ans) {
if (index == arr.size()) {
ans.push_back(arr);
return;
}
for (int i = index; i < arr.size(); i++) {
swapfunc(arr[index], arr[i]);
GetAllSort(arr, index + 1, ans);
swapfunc(arr[index], arr[i]);//同组问题应恢复现场
}
}
};
int main()
{
vector<vector<int>>ans;
vector<int>arr = { 1, 2, 3 };
Solution slt;
slt.GetAllSort(arr, 0, ans);
/******************打印结果****************/
for (int i = 0; i < ans.size(); i++) {
for (int j = 0; j < ans[0].size(); j++) {
cout << ans[i][j];
}
cout << endl;
}
cout << endl;
system("pause");
return 0;
}
全排列 递归法
最新推荐文章于 2021-05-17 04:18:55 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)