Refine solution to permutations.cpp