给定一个数字列表,返回其所有可能的排列。
class Solution {
public:
/*
* @param nums: A list of integers.
* @return: A list of permutations.
*/
vector<vector<int>> permute(vector<int> &nums) {
// write your code here
vector<vector<int>> result;
sort(nums.begin(),nums.end());
permuteRec( 0, nums, result);
return result;
}
void permuteRec( int start, vector<int> nums, vector<vector<int>> &result ){
if( start >= nums.size()){
result.push_back(nums);
return;
}
for( int i = start; i < nums.size(); i++ ){
if( i > start && nums[i] == nums[start]) continue;
swap( nums[start], nums[i] );
permuteRec( start+1, nums, result);
}
}
};