C++ solution recursive


  • 0
    D
    public:
        vector<vector<int>> permute(vector<int> nums) {
            vector<vector<int>> res;
            int size = nums.size();
            if(size >  1){
                for(int i = 0; i < size; ++i){
                    vector<int> subvec = nums;
                    vector<vector<int>> subper;
                    subvec.erase(subvec.begin()+i);
                    subper = permute(subvec);
                    for(auto j : subper){
                        j.insert(j.begin(), nums[i]);
                        res.push_back(j);
                    }
                }
            }
            if(size == 1) res.push_back(nums); 
            return res;
        }
    };

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.