Permutations Leetcode Solution
Permutations Leetcode Problem :
Given an array nums of distinct integers, return all the possible permutations. You can return the answer in any order.
Example :
Input: nums = [1,2,3]
Output: [[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]]
Permutations Leetcode Solution :
Constraints :
- 1 <= nums.length <= 6
- -10 <= nums[i] <= 10
- All the integers of nums are unique.
Example 1:
- Input: nums = [0,1]
- Output: [[0,1],[1,0]]
Example 2:
- Input: nums = [1]
- Output: [[1]]
- Constraints:
- The length of the num array is between 1 and 6.
- The Elements of the array are between -10 and 10.
- All elements of the array are different.
Intuition :
The brief explanation is the following:
- There’s a list of nums
- Our goal is to get all of the possible permutations
This can be achieved with Backtracking approach.
Approach :
- declare an empty ans
- define backtrack function
- if len(path) == len(nums), add the current permutation to ans and return
- iterate over nums, check if num in permutation, and if it isn’t, add num to permutation
- call backtrack([])
- return ans
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Code :
C++
Java
Python
C++
class Solution { public: #define DPSolver ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); void permutations(const vector< int> &nums, vector< vector< int>> &res, vector< bool> &taken, vector< int> per, const int sz, int lvl) { if (lvl == sz) { res.push_back(per); return; } for (int i = 0; i < sz; i++) { if (!taken[i]) { taken[i] = true; per.push_back(nums[i]); permutations(nums, res, taken, per, sz, lvl + 1); per.pop_back(); taken[i] = false; } } } vector< vector< int>> permute(vector< int> &nums) { DPSolver; vector< vector< int>> res; vector< bool> taken(nums.size()); vector< int> per; permutations(nums, res, taken, per, nums.size(), 0); return res; } vector< vector< int>> permuteUsingBuiltInFunction(vector< int>& nums) { // to get all the possible permutations. sort(nums.begin(), nums.end()); // creating a place in the memory to store the results in. vector< vector< int>> permus; // generating all possible combinations. do { permus.push_back(nums); } while (next_permutation(nums.begin(), nums.end())); // returning the result. return permus; } };
Java
public List< List< Integer>> subsets(int[] nums) { List< List< Integer>> list = new ArrayList<>(); Arrays.sort(nums); backtrack(list, new ArrayList<>(), nums, 0); return list; } private void backtrack(List< List< Integer>> list , List< Integer> tempList, int [] nums, int start){ list.add(new ArrayList<>(tempList)); for(int i = start; i < nums.length; i++){ tempList.add(nums[i]); backtrack(list, tempList, nums, i + 1); tempList.remove(tempList.size() - 1); } }
Python
class Solution: def permute(self, nums: List[int]) -> List[List[int]]: ans = [] def backtrack(path): if len(path) == len(nums): ans.append(path[:]) return for num in nums: if num not in path: path.append(num) backtrack(path) path.pop() backtrack([]) return ans
Get over 200+ course One Subscription
Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others
Login/Signup to comment