12 ms in-place solution, C++


  • 3
    X
    class Solution {
    private:
        vector<vector<int>> res;
    public:
        void permute_help(vector<int> &nums,int begin) {
            if(begin >= nums.size()) return;
            permute_help(nums, begin+1);
            for(int i=0; i<begin; i++){
                swap(nums[i], nums[begin]);
                res.push_back(nums);
                permute_help(nums, begin+1);
                swap(nums[i], nums[begin]);
            }
        }
        vector<vector<int>> permute(vector<int>& nums) {
            res.push_back(nums);
            permute_help(nums,1);
            return res;
        }
    };

Log in to reply
 

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