LeetCode 46: Permutations Solution

Master LeetCode problem 46 (Permutations), a medium challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.

46. Permutations

Problem Explanation

Explanation:

To generate all possible permutations of an array of distinct integers, we can use backtracking. The idea is to swap elements in the array to create different permutations. We start with the original array and recursively swap elements to generate all possible permutations. We keep track of the current index and swap elements accordingly.

  1. Start with an empty result list to store all permutations.
  2. Create a helper function that takes the current index, the array of integers, and the result list as parameters.
  3. If the current index is equal to the length of the array, add a copy of the array to the result list (a permutation).
  4. Iterate over the array starting from the current index.
  5. Swap the current element with the element at the current index.
  6. Recursively call the helper function with the incremented index.
  7. Swap back the elements to maintain the original array.
  8. After the recursion, the result list will contain all permutations.

Time complexity: O(N * N!) - where N is the number of elements in the input array. Space complexity: O(N) - additional space is used for recursion stack and result list.

Solution Code

class Solution {
    public List<List<Integer>> permute(int[] nums) {
        List<List<Integer>> result = new ArrayList<>();
        permuteHelper(nums, 0, result);
        return result;
    }
    
    private void permuteHelper(int[] nums, int index, List<List<Integer>> result) {
        if (index == nums.length) {
            List<Integer> permutation = new ArrayList<>();
            for (int num : nums) {
                permutation.add(num);
            }
            result.add(permutation);
        } else {
            for (int i = index; i < nums.length; i++) {
                swap(nums, index, i);
                permuteHelper(nums, index + 1, result);
                swap(nums, index, i);
            }
        }
    }
    
    private void swap(int[] nums, int i, int j) {
        int temp = nums[i];
        nums[i] = nums[j];
        nums[j] = temp;
    }
}

Try It Yourself

Loading code editor...

Related LeetCode Problems

Frequently Asked Questions

How to solve LeetCode 46 (Permutations)?

This page provides optimized solutions for LeetCode problem 46 (Permutations) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.

What is the time complexity of LeetCode 46 (Permutations)?

The time complexity for LeetCode 46 (Permutations) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.

Can I run code for LeetCode 46 on DevExCode?

Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 46 in Java, C++, or Python.

Back to LeetCode Solutions