46. Permutations
Given a collection of distinct numbers, return all possible permutations.
For example,
[1,2,3]
have the following permutations:
[
[1,2,3],
[1,3,2],
[2,1,3],
[2,3,1],
[3,1,2],
[3,2,1]
]
链接:https://leetcode.com/problems/permutations/description/
思路:学习一下C++中全排列函数的使用。
代码:
class Solution { public: vector<vector<int> > permute(vector<int>& nums) { vector <vector<int> > ans; sort(nums.begin(),nums.end()); do { ans.push_back(nums); }while(next_permutation(nums.begin(),nums.end())); return ans; } };