All problems
Permutations Generation
Problem
Generate every permutation of the integers in nums and return them as a list of lists in lexicographic order.
Backtrack over a sorted copy of the input, tracking which positions are already used, and append each completed arrangement. The input may be unsorted; return a list containing only the empty list when nums is empty.
Hint
Sort a copy first, then choose elements position by position with a used flag.
Test cases
case 1
in: [[1,2,3]]
expected: [[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]]
case 2
in: [[0]]
expected: [[0]]
case 3
in: [[]]
expected: [[]]
case 4
in: [[3,1,2]]
expected: [[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]]
Starter codePython
def permutations(nums):
# Your code here
pass4 test cases · run your code against them in the editor