Permutations
Return all possible permutations of the array. You may return the permutations in any order.
Examples
Input: [1,2,3]
Output: [[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]]
Input: [0,1]
Output: [[0,1],[1,0]]
Hints
Use recursion to explore all possible positions for each element in the array, swapping elements to generate permutations.
Maintain a "used" array to track which elements have been included in the current permutation, building the result incrementally.
After exploring a permutation, backtrack by undoing the last choice (e.g., swapping back or unmarking an element) to explore alternative paths.
Related Problems
Permutations
Return all possible permutations of the array. You may return the permutations in any order.