A very easy solution(using C++ and Using backtracking ideas)


  • 0
    W

    vector<vector<int>> result;
    void quan(vector<int>& nums,int n,int m)
    {
    if(n==m)
    {
    result.push_back(nums);
    }
    else
    {
    for(int i=n;i<m;++i)
    {
    swap(nums[i], nums[n]);
    quan(nums, n + 1, m);
    swap(nums[i], nums[n]);
    }
    }
    }
    vector<vector<int>> permute(vector<int>& nums) {
    int m=nums.size();
    quan(nums,0,m);
    return result;
    }


Log in to reply
 

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