c++ solution


  • 0
    B

    swapping elements in nums

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

Log in to reply
 

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