Permutations Leetcode Solution in c++ | Hindi Coding Community

0



Given an array nums of distinct integers, return all the possible permutations. You can return the answer in any order.


Example 1:


Input: nums = [1,2,3]

Output: [[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]]



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

}; 

Post a Comment

0Comments
Post a Comment (0)

#buttons=(Accept !) #days=(20)

Our website uses cookies to enhance your experience. Learn More
Accept !