46_ Permutations
[Medium][Backtracking]
Given a collection of distinct integers, return all possible permutations.
Example:
Solution: Backtracking
Idea:
Think this problems as a tree problem, put one integer in each layer and do a depth-first-search to get all the paths.
When we generate the tree, we do depth first.
Change the positions of integers so that we can create each permutation in place.
Time Complexity: because there are permutations/paths, and on each node we have constant computation, thus for each path, there are computations.
Space Complexity:
Last updated