rewrite leetcode next permutation