i' am unable to make recursion tree i saw many videos on this question but nobody is telling that after first left most leaf node completion how the full recursion is made cause after there will two variable which index and j are updated. According to me if index is initialized in j then all elements are swapped by themselves.
how the code works please explain me
Example 1:
Input: nums = [1,2,3] Output: [[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]]
class Solution {
private:
void solve(vector <int>nums, vector<vector<int>> &ans, int index){
if(index >= nums.size()){
ans.push_back(nums);
return;
}
for(int j= index; j<nums.size(); j++){
swap(nums[index] , nums[j]);
solve(nums, ans, index+1);
//backtrack
swap(nums[index], nums[j]);
}
}
public:
vector<vector<int>> permute(vector<int>& nums) {
vector<vector<int>> ans;
int index=0;
solve(nums, ans, index);
return ans;
}
};
Yes, the very first permutation that is generated is the original order of
nums. This should be included, as that is one of the valid permutations.Take a look at this loop:
The idea behind this loop is to have all values (from
indexto the end) take turns in taking the position atindex, and then generate all permutations of the "rest" of the array (fromindex+1onwards).The first iteration of the loop will choose
nums[index]to take that position, so it actually doesn't have to move. So yes, the swap is doing nothing then. That's OK. But after the recursive call has returned, and the second swap is executed (which again does not move anything), we get to the next iteration of this loop:Now
jisindex+1, and so the swap has an effect. It places the selected value (at indexj) at indexindex, and now again a recursive call is made to generate all the permutations of the "rest" of the array.Visualising
So for the example
nums = [1,2,3], we can represent what happens in a table, where the different recursion levels are each represented as a separate column. The value ofindexactually corresponds to the recursion depth (starting with 0), so I'll put that in the column headers. The last column is reserved to show the current state ofnums. It is only filled in when something important happens, like a swap or pushing that result in theansvector.I'll abbreviate some of the quoted code, so that
swap(0, 2)is short forswap(nums[0],nums[1]), andpush_backshort forans.push_back(nums), andsolve()short forsolve(nums, ans, index+1)...index==0index==1index==2index==3numsj=0[1,2,3]swap(0,0)solve()j=1swap(1,1)solve()j=2swap(2,2)solve()j=3push_back[1,2,3]*swap(2,2)j=3swap(1,1)j=2swap(1,2)[1,3,2]solve()j=2swap(2,2)solve()j=3push_back[1,3,2]*swap(2,2)j=3swap(1,2)[1,2,3]j=3swap(0,0)j=1swap(0,1)[2,1,3]solve()j=1swap(1,1)solve()j=2swap(2,2)solve()j=3push_back[2,1,3]*swap(2,2)j=3swap(1,1)j=2swap(1,2)[2,3,1]solve()j=2swap(2,2)solve()j=3push_back[2,3,1]*swap(2,2)j=3swap(1,2)[2,1,3]j=3swap(0,1)[1,2,3]j=2swap(0,2)[3,2,1]solve()j=1swap(1,1)solve()j=2swap(2,2)solve()j=3push_back[3,2,1]*swap(2,2)j=3swap(1,1)j=2swap(1,2)[3,1,2]solve()j=2swap(2,2)solve()j=3push_back[3,1,2]*swap(2,2)j=3swap(1,2)[3,2,1]j=3swap(0,2)[1,2,3]j=3The execution flow is from top to bottom. If the code is placed in the second column, it means it is executing the second call of
solve, withindexequal to 1, ...etc.Also note how each execution of
solve(each column) has its own set of local variables. This means that whenjis incremented by theforloop, this doesn't affect the value ofjin any other execution context (other column).I hope this clarifies it.