Intuitive and easy to understand C++ solution!

programming
interview-questions
Tags: #<Tag:0x00007f242f5e2568> #<Tag:0x00007f242f5e1078>

#1

void solve(int n, vector< int > &curr, vector<vector< int >> &res, vector< bool > &vis, vector< int > &a)
{

if(curr.size() == n)
{
    res.push_back(curr);
    return;
}

for(int i = 0; i < n; i++)
{
    if(vis[i] == false)
    {
        vis[i] = true;
        curr.push_back(a[i]);
        solve(n, curr, res, vis, a);
        curr.pop_back();
        vis[i] = false;
    }
}   

}

vector<vector > Solution::permute(vector &A) {

int n = A.size();
vector<int> curr;
vector<vector<int>> res;
vector<bool> vis(n, false);

solve(n, curr, res, vis, A);

return res;

}