Problem:
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]
.
解法一:使用STL的next_permutation,函数原型参照c++ reference
1 class Solution { 2 public: 3 vector<vector<int>> permute(vector<int>& nums) { 4 5 vector<vector<int>> result; 6 7 sort(nums.begin(),nums.end()); 8 9 do{ 10 11 result.push_back(nums); 12 }while(next_permutation(nums.begin(),nums.end())); 13 return result; 14 15 } 16 };
解法二:
next_permutation自己实现
http://www.cnblogs.com/xiaoying1245970347/p/4563062.html
解法三:遍历排列数后期补上。
原文:http://www.cnblogs.com/xiaoying1245970347/p/4571687.html