Problem
Given a collection of distinct integers, return all possible permutations.
Example:
1 |
|
Explanation
-
From the above example, for the first element in the sublist, we can choose $1$, $2$, or $3$, for the second number, we can still choose $1$, $2$, or $3$ but with the condition that the number we are going to add must not appear before. So, we need a for loop to iterate the number, and we need a
visited
array to keep track of the visited number. -
Base case is whenever the sublist’s length is equal to the
nums
length, then we return.
Solution
1 |
|