1
0
mirror of https://gitee.com/coder-xiaomo/leetcode-problemset synced 2025-09-13 19:31:42 +08:00
Code Issues Projects Releases Wiki Activity GitHub Gitee
Files
leetcode-problemset/leetcode-cn/problem (Chinese)/颜色分类 [sort-colors].html
2022-03-29 12:43:11 +08:00

44 lines
1.3 KiB
HTML
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<p>给定一个包含红色、白色和蓝色、共&nbsp;<code>n</code><em> </em>个元素的数组<meta charset="UTF-8" />&nbsp;<code>nums</code>&nbsp;<strong><a href="https://baike.baidu.com/item/%E5%8E%9F%E5%9C%B0%E7%AE%97%E6%B3%95" target="_blank">原地</a></strong>对它们进行排序,使得相同颜色的元素相邻,并按照红色、白色、蓝色顺序排列。</p>
<p>我们使用整数 <code>0</code>&nbsp;<code>1</code><code>2</code> 分别表示红色、白色和蓝色。</p>
<ul>
</ul>
<p>必须在不使用库的sort函数的情况下解决这个问题。</p>
<p>&nbsp;</p>
<p><strong>示例 1</strong></p>
<pre>
<strong>输入:</strong>nums = [2,0,2,1,1,0]
<strong>输出:</strong>[0,0,1,1,2,2]
</pre>
<p><strong>示例 2</strong></p>
<pre>
<strong>输入:</strong>nums = [2,0,1]
<strong>输出:</strong>[0,1,2]
</pre>
<p>&nbsp;</p>
<p><strong>提示:</strong></p>
<ul>
<li><code>n == nums.length</code></li>
<li><code>1 &lt;= n &lt;= 300</code></li>
<li><code>nums[i]</code><code>0</code><code>1</code><code>2</code></li>
</ul>
<p>&nbsp;</p>
<p><strong>进阶:</strong></p>
<ul>
<li>你可以不使用代码库中的排序函数来解决这道题吗?</li>
<li>你能想出一个仅使用常数空间的一趟扫描算法吗?</li>
</ul>