# 【LeetCode】 46. Permutations ## Description > 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] ] ``` ## Solution * 因為我覺得寫組合很麻煩,直接使用STL中algorithm內建的next_permutation。 * 記得使用前要先排序。 ### Code ```C++=1 class Solution { public: vector<vector<int>> permute(vector<int>& nums) { int size = nums.size(); vector<vector<int>> ans; sort(nums.begin(),nums.end()); do { vector<int> temp; for(int i=0;i<size;i++) temp.push_back(nums[i]); ans.push_back(temp); } while(next_permutation(nums.begin(),nums.end())); return ans; } }; ``` ###### tags: `LeetCode` `C++`