How to solve Leetcode, 46 permutation?
Let's see a easy way to solve with tree breadth first solution.
For example : [1, 2, 3]
The approach : take an item each time to insert into in between array.
1: [ [1] ]
Insert the first number.
2: [ [1] ]
^ ^
Insert 2 before and after 1.
[[2,1],[1,2]]
3:
[
[3,2,1],[2,3,1],[2,1,3],
// place 3 around and between [ 2,1 ]
^ ^ ^
[3,1,2],[1,3,2],[1,2,3]
// place 3 around and between [ 1,2 ]
^ ^ ^
]
Hope this approach is easy to understand.