> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Permutations Python Solution with Tests

> Tested Python solution for LeetCode 46 with 12 pytest cases. Generate a practice environment with lcpy.

LeetCode 46, [Medium](https://leetcode-py.wisl.dev/catalog/medium.md). Topics: [Array](https://leetcode-py.wisl.dev/catalog/topics/array), [Backtracking](https://leetcode-py.wisl.dev/catalog/topics/backtracking). [View on LeetCode](https://leetcode.com/problems/permutations/description/).

Generate this problem as a practice environment: tested reference solution, 12 [parametrized pytest cases](https://leetcode-py.wisl.dev/practice/testing.md), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 46   # by problem number
lcpy gen -s permutations   # by problem name
```

## Problem

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

### Examples

```
Input: nums = [1,2,3]
Output: [[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]]
```

```
Input: nums = [0,1]
Output: [[0,1],[1,0]]
```

```
Input: nums = [1]
Output: [[1]]
```

### Constraints

* 1 \<= nums.length \<= 6
* -10 \<= nums\[i] \<= 10
* All the integers of nums are unique.

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/permutations/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/permutations/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(n! * n)
    # Space: O(n! * n) output + O(n) recursion
    def permute(self, nums: list[int]) -> list[list[int]]:
        result = []

        def backtrack(start: int) -> None:
            if start == len(nums):
                result.append(nums[:])
                return

            for i in range(start, len(nums)):
                nums[start], nums[i] = nums[i], nums[start]
                backtrack(start + 1)
                nums[start], nums[i] = nums[i], nums[start]

        backtrack(0)
        return result
```

## Complexity

| Time | Space |
| - | - |
| O(n! \* n) | O(n! \* n) output + O(n) recursion |

## Tags

[Grind 75](https://leetcode-py.wisl.dev/catalog/grind-75.md), [Grind](https://leetcode-py.wisl.dev/catalog/grind.md), [NeetCode 150](https://leetcode-py.wisl.dev/catalog/neetcode-150.md), [NeetCode 250](https://leetcode-py.wisl.dev/catalog/neetcode-250.md), [NeetCode All](https://leetcode-py.wisl.dev/catalog/neetcode.md), [AlgoMaster 75](https://leetcode-py.wisl.dev/catalog/algo-master-75.md).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.