1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| class Solution { public: vector<vector<int>> permute(vector<int>& nums) { dfs(nums, 0); return res; } private: vector<vector<int>> res; void dfs(vector<int> nums, int x) { if (x == nums.size() - 1) { res.push_back(nums); return; } for (int i = x; i < nums.size(); i++) { swap(nums[i], nums[x]); dfs(nums, x + 1); swap(nums[i], nums[x]); } } };
|