Leet Code :Next Permutation


[Question]

Implement next permutation, which rearranges numbers into the lexicographically next greater permutation of numbers.

If such arrangement is not possible, it must rearrange it as the lowest possible order (ie, sorted in ascending order).

The replacement must be in-place, do not allocate extra memory.

Here are some examples. Inputs are in the left-hand column and its corresponding outputs are in the right-hand column.

1,2,3 → 1,3,2
3,2,1 → 1,2,3
1,1,5 → 1,5,1

[Thought]

为了得到下一个排列,要尽可能小的增长这个序列。 例如对于 01 的下一个排列,我们应该对最右边的1进行加一得到 02,而不会对左边的0进行加1得到11.也就是说我们应该对右边的低位序列进行增长,这样才能保证最小的增长。

参考ref

enter image description here

[Code]


Haiyang Xu 10 May 2014