leetCode 46 Permutations: given a collection of distinct integers, return all possible permutations.
Input: [1, 2, 3]
Output:
[ [1,2,3], [1,3,2], [2, 1, 3], [2, 3, 1], [3, 1,2],
[3,2,1] ]
At first glipmse, it’s middle difficult, jump to recursive implementation. But there always confused failures, e.g. passing vector as reference to the recursive calls, and even do a deep copy of that vector, the base vector is always modified by the copied vector. This problem blocks me a whole day, actually I don’t know how to make a recursive calls work.
tip1) each recursive fun has its independent local variables ;
tip2) recursive calls are serialized, namely only when inside recursive fun is done, then the outside recursive will continue.
tip1 helps to explain when local variables changed unexpected; tip2 helps cause I trend to think all recursive calls are parallezed running, but the real running order is serialized, so no twist.
simulate how human brain solve this problem:
<1(fix), 2(fix), 3> ; <1(fix), 3(fix), 2> …
the first position has n potentional values, then the second position has (n-1) potentional values, etc. so design the very first implementation as: traverse all potentional values in first position, and push it; then travers all potentional values in next position, and push it; till the last position, while only one element left, and push it. so a new combination is created.
|
|
the first implmenation only works for fixed input <1, 3="" 2,="">, so how to design a recursive fun to make this idea more general ? it’s easy to abstract the process above: for ith position in the new combination vector, the potentional values is one less from (i-1)th position, traverse all possible values in ith position and push it, don’t forget pop it out for traversing to the next possible value; and the end of recursive is when only one element left.1,>
|
|
test:
|
|