I came up with this code which got accepted. Problem is that this doesn't produce a lexicographical permutation.

```
void perm(vector<int> &nums, vector<int> &row,vector<vector<int> > &result, int k) {
if (k == nums.size()-1) {
row = nums;
result.push_back(row);
return ;
}
for(int i = k; i<nums.size();++i) {
swap(nums[i],nums[k]);
perm(nums,row,result,k+1);
swap(nums[i],nums[k]);
}
}
class Solution {
public:
vector<vector<int>> permute(vector<int>& nums) {
vector<int> row;
vector<vector<int> > result;
perm(nums,row,result,0);
return result;
}
};
```

I checked the top solution and it is very similar to what I had done and executed that. Even that solution doesn't produce a correct lexicographical order.

Question is how can this code be modified if lexicographical ordering is a must?

Edit:

By lexicographical ordering, I mean my output is currently producing this:

```
[[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,2,1],[3,1,2]]
```

However, the correct ordering should be

```
[[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]]
```

Notice the 312 vs 321 at the end. It becomes more apparent which the input size is increased.