Permutations LeetCode Solution

Here, We see Permutations problem Solution. This Leetcode problem is done in many programming languages like C++, Java, JavaScript, Python, etc., with different approaches.

Permutations LeetCode Solution

Permutations LeetCode Solution

Problem Statement ->

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]]

Example 2:
Input: nums = [0,1]
Output: [[0,1],[1,0]]
Example 3:
Input: nums = [1]
Output: [[1]]

Permutations Leetcode Solution C++ ->

class Solution {
public:
    vector<vector<int>> permute(vector<int>& nums) {
        vector<vector<int>> results;
        generatePermutations(0, &nums, &results);
        return results;
    }
private:
    void generatePermutations(int i, vector<int>* nums_ptr, vector<vector<int>>* results) {
        auto& nums = *nums_ptr;
        if (i == nums.size() - 1) {
            results->emplace_back(nums);
            return;
        }
        for (int j = i; j < nums.size(); ++j) {
            std::swap(nums[i], nums[j]);
            generatePermutations(i + 1, nums_ptr, results);
            std::swap(nums[i], nums[j]);
        }       
    }
};
Code language: C++ (cpp)

Permutations Leetcode Solution Java ->

class Solution {
    public List<List<Integer>> permute(int[] nums) {
		List<List<Integer>> permutations = new ArrayList<>();
		if (nums.length == 0) {
			return permutations;
		}
		collectPermutations(nums, 0, new ArrayList<>(), permutations);
		return permutations;
    }
	private void collectPermutations(int[] nums, int start, List<Integer> permutation,
 			List<List<Integer>>  permutations) {
		
		if (permutation.size() == nums.length) {
			permutations.add(permutation);
			return;
		}
		for (int i = 0; i <= permutation.size(); i++) {
			List<Integer> newPermutation = new ArrayList<>(permutation);
			newPermutation.add(i, nums[start]);
			collectPermutations(nums, start + 1, newPermutation, permutations);
		}     
    }
}
Code language: Java (java)

Permutations Leetcode Solution JavaScript ->

var permute = function(nums) {
  let results = [];

  let permutations = (current, remaining) => {
    if(remaining.length <= 0) results.push(current.slice());
    else {
      for(let i = 0; i < remaining.length; i++) { // Loop through remaining elements
        current.push(remaining[i]); // Insert the iTH element onto the end of current
        permutations(current.slice(), remaining.slice(0, i).concat(remaining.slice(i+1))); // Recurse with inserted element removed
        current.pop(); // Remove last inserted element for next iteration
      }
    }
  };
  permutations([], nums);
  return results;  
};
Code language: JavaScript (javascript)

Permutations Leetcode Solution Python ->

class Solution(object):
    def permute(self, nums):
        res = []
        self.dfs(nums, [], res)
        return res
    def dfs(self, nums, path, res):
        if not nums:
            res.append(path)
        for i in xrange(len(nums)):
            self.dfs(nums[:i]+nums[i+1:], path+[nums[i]], res)
Code language: Python (python)
Scroll to Top