My C++ recursion solution


  • 0
    D
    void recursion(vector<int> &nums,int len,int index,vector<vector<int>> &res) {
        if (index == len) {
            res.push_back(nums);
        }
        else {
            recursion(nums,len,index+1,res);
            for (int i = index+1;i <= len;++i) {
                swap(nums[index],nums[i]);
                recursion(nums,len,index+1,res);
                swap(nums[index],nums[i]);
            }
        }
        
    }
    vector<vector<int>> permute(vector<int>& nums) {
        int len = nums.size()-1;
        int index = 0;
        vector<vector<int>> res;
        recursion(nums,len,index,res);
        return res;
    }

Log in to reply
 

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