# 全排列

给定一个 没有重复 数字的序列,返回其所有可能的全排列。

示例:

输入: [1,2,3]
输出:
[ [1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], [3,2,1]]
以下程序实现了这一功能,请你填补空白处内容: ```cpp #include using namespace std; class Solution { public: vector> permute(vector &nums) { vector> res; vector used(nums.size()); dfs(nums, used, res); return res; } private: vector stack; void dfs(vector &nums, vector &used, vector> &res) { if (stack.size() == nums.size()) { res.push_back(stack); } else { for (int i = 0; i < nums.size(); i++) { if (!used[i]) { _______________; } } } } }; ``` ## template ```cpp #include using namespace std; class Solution { public: vector> permute(vector &nums) { vector> res; vector used(nums.size()); dfs(nums, used, res); return res; } private: vector stack; void dfs(vector &nums, vector &used, vector> &res) { if (stack.size() == nums.size()) { res.push_back(stack); } else { for (int i = 0; i < nums.size(); i++) { if (!used[i]) { used[i] = true; stack.push_back(nums[i]); dfs(nums, used, res); stack.pop_back(); used[i] = false; } } } } }; ``` ## 答案 ```cpp used[i] = true; stack.push_back(nums[i]); dfs(nums, used, res); stack.pop_back(); used[i] = false; ``` ## 选项 ### A ```cpp used[i] = false; stack.push_back(nums[i]); dfs(nums, used, res); stack.pop_back(); used[i] = true; ``` ### B ```cpp stack.push_back(nums[i]); dfs(nums, used, res); stack.pop_back(); used[i] = true; ``` ### C ```cpp stack.push_back(nums[i]); dfs(nums, used, res); stack.pop_back(); ```