# [75. Sort Colors (Medium)](https://leetcode.com/problems/sort-colors/)
<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>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>Example 2:</strong></p>
<pre><strong>Input:</strong> nums = [2,0,1]
<strong>Output:</strong> [0,1,2]
</pre><p><strong>Example 3:</strong></p>
<pre><strong>Input:</strong> nums = [0]
<strong>Output:</strong> [0]
</pre><p><strong>Example 4:</strong></p>
<pre><strong>Input:</strong> nums = [1]
<strong>Output:</strong> [1]
</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 <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>
**Companies**:
[Microsoft](https://leetcode.com/company/microsoft), [Amazon](https://leetcode.com/company/amazon), [Adobe](https://leetcode.com/company/adobe), [Apple](https://leetcode.com/company/apple), [Facebook](https://leetcode.com/company/facebook), [Bloomberg](https://leetcode.com/company/bloomberg), [Nvidia](https://leetcode.com/company/nvidia), [Swiggy](https://leetcode.com/company/swiggy)
**Related Topics**:
[Array](https://leetcode.com/tag/array/), [Two Pointers](https://leetcode.com/tag/two-pointers/), [Sorting](https://leetcode.com/tag/sorting/)
**Similar Questions**:
* [Sort List (Medium)](https://leetcode.com/problems/sort-list/)
* [Wiggle Sort (Medium)](https://leetcode.com/problems/wiggle-sort/)
* [Wiggle Sort II (Medium)](https://leetcode.com/problems/wiggle-sort-ii/)
## Solution 1. Count Sort
```cpp
// OJ: https://leetcode.com/problems/sort-colors/
// Author: github.com/lzl124631x
// Time: O(N)
// Space: O(1)
class Solution {
public:
void sortColors(vector<int>& nums) {
vector<int> cnt(3, 0);
for (int n : nums) cnt[n]++;
int i = 0;
for (int j = 0; j < 3; ++j) {
while (cnt[j]--) nums[i++] = j;
}
}
};
```
## Solution 2.
```cpp
// OJ: https://leetcode.com/problems/sort-colors/
// Author: github.com/lzl124631x
// Time: O(N)
// Space: O(1)
class Solution {
public:
void sortColors(vector<int>& nums) {
int r = 0, g = 0, b = 0;
for (int n : nums) {
if (n == 0) {
nums[b++] = 2;
nums[g++] = 1;
nums[r++] = 0;
} else if (n == 1) {
nums[b++] = 2;
nums[g++] = 1;
} else nums[b++] = 2;
}
}
};
```
## Solution 3.
[Dutch national flag problem](https://en.wikipedia.org/wiki/Dutch_national_flag_problem)
```cpp
// OJ: https://leetcode.com/problems/sort-colors/
// Author: github.com/lzl124631x
// Time: O(N)
// Space: O(1)
// Ref: https://leetcode.com/problems/sort-colors/solution/
class Solution {
public:
void sortColors(vector<int>& A) {
int zero = 0, two = A.size() - 1;
for (int i = 0; i <= two; ) {
if (A[i] == 0) {
swap(A[i++], A[zero++]);
} else if (A[i] == 2) {
swap(A[i], A[two--]);
} else ++i;
}
}
};
```
c++-c++编程基础之leetcode题解第75题颜色分类.zip
需积分: 1 147 浏览量
2024-04-16
08:23:22
上传
评论
收藏 2KB ZIP 举报
m0_57195758
- 粉丝: 762
- 资源: 252
最新资源
- 基于matlab实现车牌识别程序,和论文,自己做的,做毕业设计的可以看看 .rar
- Windows系统下安装与配置Neo4j的步骤
- 基于matlab实现潮流计算和最优潮流计算的程序1,对毕业设计有一定用处.rar
- 基于大数据学习资源推荐系统的设计与实现(部署视频)-kaic.mp4
- 哈工大形式语言和自动机2022期末含答案
- Windows系统下安装与配置Neo4j的步骤
- 哈希算法(Hash Algorithm)是一种将任意长度的二进制数据映射为较短的、固定长度的二进制值的函数.txt
- Windows系统下安装与配置Neo4j的步骤
- 在二叉树或更复杂的树形结构中,先序输出叶结点.txt
- 列出所有祖先结点的概念通常与树形结构或图论中的节点相关.txt
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈