from http://blog.csdn.net/so_geili/article/details/71078945
- Find the largest index
k
such thatnums[k] < nums[k + 1]
. If no such index exists, the permutation is sorted in descending order, just reverse it to ascending order and we are done. For example, the next permutation of[3, 2, 1]
is[1, 2, 3]
. - Find the largest index
l
greater thank
such thatnums[k] < nums[l]
. - Swap the value of
nums[k]
with that ofnums[l]
. - Reverse the sequence from
nums[k + 1]
up to and including the final elementnums[nums.size() - 1]
.