#75

Sort Colors

pupil · 570 · lc medium +29 · verified · 69.2% accepted · 21,455 likes · top 77%

play →

Description

Sort array nums of n items representing red (0), white (1), and blue (2) objects in-place so all reds precede whites, which precede blues. The standard library sort function may not be used.

Example 1:

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

Example 2:

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

Code

1
2
3
4
5
6