You are given an integer array nums in which every element is 0, 1, or 2. Return the values rearranged into non-decreasing order: all the 0s first, then all the 1s, then all the 2s.
The returned array has the same length as nums and the same number of each value, so only one arrangement is correct.
Examples
Example 1
Input: nums = [2,0,2,1,1,0]
Output: [0,0,1,1,2,2]
Explanation: The array holds two 0s, two 1s and two 2s, so they come back grouped in that order.
Example 2
Input: nums = [2,0,1]
Output: [0,1,2]
Explanation: One copy of each value, returned smallest to largest.
Constraints
n==nums.length
1≤n≤105
nums[i] is either 0, 1, or 2
Loading editor...
Log in to run your code. Your draft is saved in this browser.Log inSign up