Sort Colors

Given an array containing only 0s, 1s, and 2s, sort the array in-place so that values appear in the order 0, 1, 2. Because the value range is tiny, counting or Dutch National Flag style partitioning is ideal.

Input Format

nums = array containing only 0, 1, and 2

Output Format

nums sorted in-place in 0-1-2 order

Constraints

  • 1 <= nums.length <= 3 * 10^5; nums[i] in {0,1,2}

Examples

Example 1:

Input:

nums = [2,0,2,1,1,0]

Output:

[0,0,1,1,2,2]

Explanation:

The array is grouped by its three bounded values.

Example 2:

Input:

nums = [2,0,1]

Output:

[0,1,2]

Explanation:

A single pass can place each value into the correct region.

Loading...
Sort Colors - Sorting Based Array Problems