Permutations

Medium Backtracking

nums is a list of different whole numbers.

A permutation of nums is a list that holds the same numbers in some order, each one used exactly once.

Return every permutation of nums, each as a list. The permutations may come in any order, but the order of the numbers inside each one matters, and no permutation may appear twice.

Example 1

Input: nums = [5,1,8]
Output: [[5,1,8],[5,8,1],[1,5,8],[1,8,5],[8,5,1],[8,1,5]]

Three different numbers can be put in order in 3 × 2 × 1 = 6 ways, and every one of them is a permutation.

Example 2

Input: nums = [2,9]
Output: [[2,9],[9,2]]

Example 3

Input: nums = [-4]
Output: [[-4]]

Constraints

  • 1 ≤ nums.length ≤ 8
  • -100 ≤ nums[i] ≤ 100
  • All the numbers in nums are different

Practice this in an AI round

30 minutes on this problem with an AI agent beside your code, then a report on your solution and how you used the agent.

See the AI round

Watch the explanation

More Backtracking

All Backtracking problems