Leetcode Problem.46—Permutations C++实现
Given a collection of 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]
, and [3,2,1]
.
中文描述:将数组实现全排列。
我的C++程序:
STL algorithm 实现 next-permutation
vector<vector<int>> permute(vector<int>& nums)
{
int len=nums.size();
vector<vector<int>> result;
vector <int>temp;
sort(nums.begin(),nums.end());
do {
for(int i=0;i<len;i++)
temp.push_back(nums[i]);
result.push_back(temp);
temp.clear();
} while (next_permutation(nums.begin(),nums.end()));
return result;
}
版权声明:本文为xingyanxiao原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。