Sort Colors

Medium Two pointers Array

Problem

An array contains only the values 0, 1 and 2. Sort it in place so the zeros come first, then the ones, then the twos.

Original: LeetCode 75: Sort Colors.

Examples

Example 1

Input`nums = [2, 0, 2, 1, 1, 0]`
Output`nums = [0, 0, 1, 1, 2, 2]`

Example 2

Input`nums = [2, 0, 1]`
Output`nums = [0, 1, 2]`

Constraints

  • n == nums.length
  • 1 <= n <= 300
  • nums[i] is either 0, 1, or 2.

Hints

Solution

Tried it yourself? The approach, complexity, and code stay hidden until you choose.

Sources (1)
  1. LeetCode 75: Sort Colorsleetcode.com