Next Permutation

Medium

Topics
ArrayTwo Pointers

Rearrange nums into the lexicographically next greater permutation. If no greater permutation exists, rearrange to the lowest order (sorted ascending). Return the resulting array. Do it in-place with O(1) extra memory.

Example 1

Input:  nums = [1,2,3]
Output: [1,3,2]

Example 2

Input:  nums = [3,2,1]
Output: [1,2,3]

Example 3

Input:  nums = [1,1,5]
Output: [1,5,1]

Constraints

  • 1 <= nums.length <= 100
  • 0 <= nums[i] <= 100
Run ⌘' · Submit ⌘⏎