Logo
IconChallenge
Icon
IconProblem
IconSolutions
IconSubmissions

Sort Colors

XPChallenge Points: 20
levelLevel: Medium

You are given an array nums containing 0s, 1s, and 2s, representing three colors.
Sort the array in-place so that all 0s come first, then 1s, and then 2s.
Do not use any built-in sort function.

Example 1:

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

Output: [0,0,1,1,1,2,2]

Explanation:

All the 0s are first, followed by 1s, then 2s.

Example 2:

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

Output: [0,1,1,2]

to Continue
like
dislike

Accepted:

Submission:

IconReport an issue
Icon
IconCode
IconYou need toto run or submitYou need toto run or submit
IconTest Case
IconTest Result