Nav: << previous: 74.搜索二维矩阵 | next: 76.最小覆盖子串 >>
Description
tab: English
<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>
<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>
<p>You must solve this problem without using the library's sort function.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [2,0,2,1,1,0]
<strong>Output:</strong> [0,0,1,1,2,2]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [2,0,1]
<strong>Output:</strong> [0,1,2]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == nums.length</code></li>
<li><code>1 <= n <= 300</code></li>
<li><code>nums[i]</code> is either <code>0</code>, <code>1</code>, or <code>2</code>.</li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> Could you come up with a one-pass algorithm using only constant extra space?</p>
> [!tip]- Hint 1
>
> A rather straight forward solution is a two-pass algorithm using counting sort.
> [!tip]- Hint 2
>
> Iterate the array counting number of 0's, 1's, and 2's.
> [!tip]- Hint 3
>
> Overwrite array with the total number of 0's, then 1's and followed by 2's.
---
[submissions](https://leetcode.com/problems/sort-colors/submissions/) | [solutions](https://leetcode.com/problems/sort-colors/solutions/)
tab: 中文
<p>给定一个包含红色、白色和蓝色、共 <code>n</code><em> </em>个元素的数组<meta charset="UTF-8" /> <code>nums</code> ,<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>、 <code>1</code> 和 <code>2</code> 分别表示红色、白色和蓝色。</p>
<ul>
</ul>
<p>必须在不使用库内置的 sort 函数的情况下解决这个问题。</p>
<p> </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> </p>
<p><strong>提示:</strong></p>
<ul>
<li><code>n == nums.length</code></li>
<li><code>1 <= n <= 300</code></li>
<li><code>nums[i]</code> 为 <code>0</code>、<code>1</code> 或 <code>2</code></li>
</ul>
<p> </p>
<p><strong>进阶:</strong></p>
<ul>
<li>你能想出一个仅使用常数空间的一趟扫描算法吗?</li>
</ul>
> [!tip]- 提示 1
>
> A rather straight forward solution is a two-pass algorithm using counting sort.
> [!tip]- 提示 2
>
> Iterate the array counting number of 0's, 1's, and 2's.
> [!tip]- 提示 3
>
> Overwrite array with the total number of 0's, then 1's and followed by 2's.
---
[提交记录](https://leetcode.cn/problems/sort-colors/submissions/) | [题解](https://leetcode.cn/problems/sort-colors/solution/)
Solutions & Notes
properties:
note.updated:
displayName: Last Updated
note.relative_links:
displayName: Related Links
note.desc:
displayName: Description
note.grade:
displayName: Rating
note.program_language:
displayName: Language
note.time_complexity:
displayName: TC
note.space_complexity:
displayName: SC
views:
- type: table
name: Solutions & Notes
filters:
and:
- file.hasLink(this.file)
- file.tags.containsAny("leetcode/solution", "leetcode/note")
order:
- file.name
- desc
- program_language
- time_complexity
- space_complexity
- grade
- relative_links
- updated
sort:
- property: grade
direction: ASC
- property: time_complexity
direction: ASC
- property: program_language
direction: ASC
columnSize:
file.name: 104
note.space_complexity: 65
note.grade: 126
Similar Problems
properties:
note.lcTopics:
displayName: Topics
note.lcAcRate:
displayName: AC Rate
note.favorites:
displayName: Favorites
note.grade:
displayName: Rating
note.translatedTitle:
displayName: Title (CN)
note.lcDifficulty:
displayName: Difficulty
views:
- type: table
name: Similar Problems
filters:
and:
- file.hasLink(this.file)
- similarQuestions.contains(this.file)
order:
- file.name
- translatedTitle
- lcTopics
- lcDifficulty
- lcAcRate
- grade
- favorites
sort:
- property: file.name
direction: ASC
- property: lcTopics
direction: DESC
columnSize:
note.translatedTitle: 240
note.lcTopics: 347
note.lcAcRate: 75
note.grade: 122