31. Next Permutation
Problem
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
Related Topics:
Array
Analysis
首先明确:
当序列是降序时(最大序列),不存在比这个序列大的序列
当序列是升序时(最小序列),不存在比这个序列小的序列
从后往前遍历数组,找到第一个破坏降序的数,将其与降序中,仅比自身大的数,进行交换(此时序列已大于原序列),然后将降序变为升序即可。
Code
Last updated