Vectors are passed by value in functions: No backtracking required!


#1
void permutations(vector<int> A, vector<vector<int>> &res, int i){
    int n=A.size();
    if(i==n){
        res.push_back(A);
    }
    for(int j=i; j<n; j++){
        swap(A[i], A[j]);
        permutations(A, res, i+1);
    }
}

vector<vector<int> > Solution::permute(vector<int> &A){
    vector<vector<int>> res;
    permutations(A, res, 0);
    return res;
}