solve Permutations II using the idea of next_premutation algorithm