Top 150 InterviewEasy

Permutations

Detailed guide and Python implementation for the 'Permutations' problem.

Problem Statement

Easy

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

Implement a function permute(nums: list) -> list.

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

Examples

Example 1
Input
[1,2,3]
Output
[[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]]
Explanation

All 3! = 6 permutations of [1,2,3] are generated.

Example 2
Input
[0,1]
Output
[[0,1],[1,0]]
Explanation

All 2! = 2 permutations of [0,1] are generated.

Example 3
Input
[1]
Output
[[1]]
Explanation

Only one permutation exists for a single element.

Need a Hint?
Consider using Backtracking-specific data structures like sets or heaps.
Edge Cases to Watch
  • Empty input structures
  • Single element inputs
  • Large numerical bounds

Ready to Solve?

Open the problem in PyRun's browser-based Python editor. Your code runs fully offline — no server required.

Open in Editor

Recommended Python Resources

Expand your knowledge with related interactive tutorials, cheat sheets, and code comparisons.