Skip to content

Latest commit

 

History

History
25 lines (25 loc) · 800 Bytes

63.md

File metadata and controls

25 lines (25 loc) · 800 Bytes

Permutation in Array

LeetCode Link

    void permut(vector<int>& nums,vector<vector<int>>&ans,vector<int>&temp,vector<bool>visited){
        if(visited.size()==temp.size()){
              ans.push_back(temp);
              return;
        }
        for(int i=0;i<visited.size();i++){
            if(visited[i]==0){
                visited[i]=1;
                temp.push_back(nums[i]);
                permut(nums,ans,temp,visited);
                visited[i]=0;
                temp.pop_back();
            }
        }
    }
    vector<vector<int>> permute(vector<int>& nums) {
        vector<vector<int>>ans;
        vector<int>temp;
        vector<bool>visited(nums.size(),0);
        permut(nums,ans,temp,visited);
        return ans;
    }