solution.md 4.1 KB
Newer Older
每日一练社区's avatar
每日一练社区 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
# 颜色分类
<p>给定一个包含红色、白色和蓝色,一共 <code>n</code><em> </em>个元素的数组,<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> </p><p><strong>示例 1:</strong></p><pre><strong>输入:</strong>nums = [2,0,2,1,1,0]<strong><br />输出:</strong>[0,0,1,1,2,2]</pre><p><strong>示例 2:</strong></p><pre><strong>输入:</strong>nums = [2,0,1]<strong><br />输出:</strong>[0,1,2]</pre><p><strong>示例 3:</strong></p><pre><strong>输入:</strong>nums = [0]<strong><br />输出:</strong>[0]</pre><p><strong>示例 4:</strong></p><pre><strong>输入:</strong>nums = [1]<strong><br />输出:</strong>[1]</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>	<li>你能想出一个仅使用常数空间的一趟扫描算法吗?</li></ul>
<p>以下错误的选项是?</p>
## aop
### before
```cpp
#include <bits/stdc++.h>
using namespace std;

```
### after
```cpp
int main()
{
    Solution sol;
    int a = 3, b = 4;

    vector<int> nums = {2, 0, 2, 1, 1, 0};
    sol.sortColors(nums);
    for (auto i : nums)
        cout << i << " ";
    return 0;
}
```

## 答案
```cpp
class Solution
{
public:
    void sortColors(vector<int> &nums)
    {
        int zero = 0, two = nums.size() - 1;
        int i = 0;
        while (i <= two)
        {
            if (nums[i] <= 1)
            {
                int temp = nums[i];
                nums[i] = nums[zero];
                nums[zero] = temp;
                zero++;
                i++;
            }
            else
            {
                int temp = nums[i];
                nums[i] = nums[two];
                nums[two] = temp;
                two--;
            }
        }
    }
};
```
## 选项

### A
```cpp
class Solution
{
public:
    void sortColors(vector<int> &nums)
    {
        int p0 = 0;
        int p1 = 0;
        int p2 = nums.size() - 1;
        while (p1 <= p2)
        {
            if (nums[p1] == 0)
                swap(nums[p1++], nums[p0++]);
            else if (nums[p1] == 2)
                swap(nums[p1], nums[p2--]);
            else
                p1++;
        }
    }
};
```

### B
```cpp
class Solution
{
public:
    void sortColors(vector<int> &nums)
    {
        int a[3] = {0};
        int b[3] = {0, 1, 2};
        int num = 0;
        for (int i = 0; i < nums.size(); i++)
        {
            if (nums[i] == 0)
                a[0]++;
            if (nums[i] == 1)
                a[1]++;
            if (nums[i] == 2)
                a[2]++;
        }
        for (int i = 0; i < 3; i++)
        {
            for (int j = 0; j < a[i]; j++)
            {
                nums[num] = b[i];
                num++;
            }
        }
    }
};
```

### C
```cpp
class Solution
{
public:
    void sortColors(vector<int> &nums)
    {
        int start = 0;
        int end = nums.size() - 1;
        int temp;
        for (int i = 0; i < nums.size(); i++)
        {
            if (i > end)
                break;
            if (nums[i] == 2)
            {
                if (i == end)
                    continue;
                temp = nums[i];
                nums[i] = nums[end];
                nums[end] = temp;
                end--;
                i--;
            }
            else if (nums[i] == 0)
            {
                if (i == start)
                    continue;
                temp = nums[i];
                nums[i] = nums[start];
                nums[start] = temp;
                start++;
                i--;
            }
        }
    }
};
```