← 返回 amazon 的题目列表In-place Array Reordering (0/1/2 sort variant)
类型:online_judge
Given an integer array nums containing only 0, 1, and 2, sort the array in-place so that elements of the same value are adjacent, in the order 0, then 1, then 2.
Requirements:
Must be in-place with only O(1) extra space.
Target time complexity: O(n).
Input Format
Line 1: integer n
Line 2: n integers (each is 0/1/2) representing nums
Output Format
Print the sorted array on one line, space-separated.
Constraints
1 <= n <= 2 * 10^5
Example
Input
6
2 0 2 1 1 0
Output
0 0 1 1 2 2