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.

roman number

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 :
  1. declare an empty ans
  2. define backtrack function
  3. if len(path) == len(nums), add the current permutation to ans and return
  4. iterate over nums, check if num in permutation, and if it isn’t, add num to permutation
  5. call backtrack([])
  6. return ans
permutation leetcode

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Code :

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

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription