83. Next Permutation

MediumArrayArray

Rearrange nums into the lexicographically next greater permutation. If already the greatest (descending), rearrange to the smallest (ascending). In-place, O(1) extra memory.

Input: Integer array nums.

Output: Array rearranged to next permutation.

Examples

Example 1
Input: [1,2,3]
Output: [1,3,2]
Explanation: Next permutation after [1,2,3].
Example 2
Input: [3,2,1]
Output: [1,2,3]
Explanation: No next — wrap to smallest.
Example 3
Input: [1,1,5]
Output: [1,5,1]
Explanation: Next after [1,1,5].

Constraints

Asked by

MetaAdobeBloombergMicrosoftInfosysAmazon
Solve this problem in the editor →