|
| 1 | +<h2>75. Sort Colors</h2><h3>Medium</h3><hr><div><p>Given an array <code>nums</code> with <code>n</code> objects colored red, white, or blue, sort them <strong><a href="https://en.wikipedia.org/wiki/In-place_algorithm" target="_blank">in-place</a> </strong>so that objects of the same color are adjacent, with the colors in the order red, white, and blue.</p> |
| 2 | + |
| 3 | +<p>We will use the integers <code>0</code>, <code>1</code>, and <code>2</code> to represent the color red, white, and blue, respectively.</p> |
| 4 | + |
| 5 | +<p>You must solve this problem without using the library's sort function.</p> |
| 6 | + |
| 7 | +<p> </p> |
| 8 | +<p><strong>Example 1:</strong></p> |
| 9 | +<pre><strong>Input:</strong> nums = [2,0,2,1,1,0] |
| 10 | +<strong>Output:</strong> [0,0,1,1,2,2] |
| 11 | +</pre><p><strong>Example 2:</strong></p> |
| 12 | +<pre><strong>Input:</strong> nums = [2,0,1] |
| 13 | +<strong>Output:</strong> [0,1,2] |
| 14 | +</pre><p><strong>Example 3:</strong></p> |
| 15 | +<pre><strong>Input:</strong> nums = [0] |
| 16 | +<strong>Output:</strong> [0] |
| 17 | +</pre><p><strong>Example 4:</strong></p> |
| 18 | +<pre><strong>Input:</strong> nums = [1] |
| 19 | +<strong>Output:</strong> [1] |
| 20 | +</pre> |
| 21 | +<p> </p> |
| 22 | +<p><strong>Constraints:</strong></p> |
| 23 | + |
| 24 | +<ul> |
| 25 | + <li><code>n == nums.length</code></li> |
| 26 | + <li><code>1 <= n <= 300</code></li> |
| 27 | + <li><code>nums[i]</code> is <code>0</code>, <code>1</code>, or <code>2</code>.</li> |
| 28 | +</ul> |
| 29 | + |
| 30 | +<p> </p> |
| 31 | +<p><strong>Follow up:</strong> Could you come up with a one-pass algorithm using only constant extra space?</p> |
| 32 | +</div> |
0 commit comments