Problem: Permutations
Given a collection of distinct numbers, return all possible permutations.
class Solution {
public:
vector<vector<int>> permute(vector<int>& nums) {
int arrayLength = nums.size();
vector< vector<int> > allPossiblePermutation;
recursiveCall(0, arrayLength, allPossiblePermutation, nums);
return allPossiblePermutation;
}
void recursiveCall(int left, int right, vector<vector<int>>& allPossiblePermutation, vector<int>& nums)
{
if(left==right-1)
{
allPossiblePermutation.push_back(nums);
return;
}
for(int i=left;i<right;i++)
{
swap(nums[left], nums[i]);
recursiveCall(left+1, right, allPossiblePermutation, nums);
swap(nums[left], nums[i]);
}
}
};
No comments:
Post a Comment