题目:
Given a collection of distinct integers, return all possible permutations.
Example:
Input: [1,2,3] Output: [ [1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], [3,2,1] ]
代码:
class Solution { public: vector<vector<int>> permute(vector<int>& nums) { vector<vector<int> > res; deal(res, vector<int>(), nums); return res; } void deal(vector<vector<int> >& res, vector<int> temp, vector<int> nums){ if(temp.size() == nums.size()){ res.push_back(temp); } for(int i = 0; i < nums.size(); i++){ bool flag = true; for(int j = 0; j < temp.size(); j++){ if(temp[j] == nums[i]){ flag = false; } } if(flag){ temp.push_back(nums[i]); deal(res, temp, nums); temp.pop_back(); } } } };