Last Updated: November 14, 2025
Given an array nums of distinct integers, return all the possible permutations. You can return the answer in any order.
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]]
1 <= nums.length <= 6-10 <= nums[i] <= 10nums are unique.The basic idea for generating all permutations of an array is to swap each element with every other element and then recursively call the function for the next depth. The permutations are generated by exploring the problem's decision tree depth-first. In each recursive call, we will swap elements and call the function for the next element. Once all elements are considered, we add the current permutation into our result list.
nums. We have n! permutations and for each permutation, we copy the permutation list to results.This approach builds permutations iteratively by repeatedly inserting a new element into existing permutations generated so far. It makes use of lists and creates new permutations by inserting the current element in every possible position within each existing permutation.
nums. This is because for each new number, all previous permutations are expanded.