← 返回 meta 的题目列表Next Permutation (Array In-Place)
类型:online_judge
Problem: Next Permutation (In-Place)
Given an integer array nums of length n, modify it in-place to become the next lexicographically greater permutation.
If such a permutation exists, update nums to that permutation.
If nums is already the largest permutation in lexicographic order, update nums to the smallest permutation (i.e., sorted ascending).
Requirements
Must be in-place; extra space should be O(1).
Expected time complexity O(n).
Input/Output Format (for coding pad / script)
Input:
Line 1: integer n
Line 2: n integers representing nums
Output:
One line of n integers: the updated array.
Constraints
1 <= n <= 2 * 10^5
-10^9 <= nums[i] <= 10^9
Example
Input:
3
1 2 3
Output:
1 3 2
Example
Input
3
1 2 3
Output
1 3 2