Permutations
Write the code, see it run, pass the checks.
The problem
- medium
- Code
Write permutations(nums): nums holds 1 to 7 different whole numbers. Return an array of every ordering of them, each one an array holding all of nums, so n numbers have n factorial orderings. The orderings can come in any order. Don't change nums. For example, permutations([1, 2, 3]) could return [[1, 2, 3], [1, 3, 2], [2, 1, 3], [2, 3, 1], [3, 1, 2], [3, 2, 1]]. Build each ordering one position at a time, trying every number not used yet, and undo the choice before trying the next.
Your code must pass
- Lists every ordering of [1, 2, 3]
- Handles a single number
- Lists all 5,040 orderings of 7 numbers
- Leaves nums unchanged