求一个序列的全排列
DFS
class Solution {
public:
vector<vector<int>> ans;
vector<bool> vis{0, 0, 0, 0, 0, 0, 0};
void permutation(vector<int> per, vector<int> nums)
{
int len = nums.size();
if (per.size() == len) {
ans.push_back(per);
return;
}
for (int i = 0; i < len; i++) {
if (!vis[i]) {
vis[i] = true;
per.push_back(nums[i]);
permutation(per, nums);
per.pop_back();
vis[i] = false;
}
}
}
vector<vector<int>> permute(vector<int>& nums) {
vector<int> per;
permutation(per, nums);
return ans;
}
};