{
  "type": "code_options",
  "author": "https://github.com/begeekmyfriend/leetcode",
  "source": "solution.md",
  "exercise_id": "85a319fbe4b643e0b11939d9434f8a3e",
  "keywords": "数组,双指针,排序",
  "title": "颜色分类",
  "desc": [
    {
      "content": "\n<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>\t<li><code>n == nums.length</code></li>\t<li><code>1 <= n <= 300</code></li>\t<li><code>nums[i]</code> 为 <code>0</code>、<code>1</code> 或 <code>2</code></li></ul><p> </p><p><strong>进阶：</strong></p><ul>\t<li>你可以不使用代码库中的排序函数来解决这道题吗？</li>\t<li>你能想出一个仅使用常数空间的一趟扫描算法吗？</li></ul>",
      "language": "markdown"
    }
  ],
  "answer": [
    {
      "content": "",
      "language": "cpp"
    }
  ],
  "prepared": [
    [
      {
        "content": "",
        "language": "cpp"
      }
    ],
    [
      {
        "content": "",
        "language": "cpp"
      }
    ],
    [
      {
        "content": "",
        "language": "cpp"
      }
    ]
  ],
  "template": {
    "content": "#include <bits/stdc++.h>\nusing namespace std;\nclass Solution\n{\npublic:\n\tvoid sortColors(vector<int> &nums)\n\t{\n\t\tint i = 0, j = nums.size() - 1;\n\t\twhile (i < j)\n\t\t{\n\t\t\tif (nums[i] == 0)\n\t\t\t{\n\t\t\t\ti++;\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\tif (nums[j] != 0)\n\t\t\t{\n\t\t\t\tj--;\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\tswap(nums[i], nums[j]);\n\t\t}\n\t\tj = nums.size() - 1;\n\t\twhile (i < j)\n\t\t{\n\t\t\tif (nums[i] == 1)\n\t\t\t{\n\t\t\t\ti++;\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\tif (nums[j] != 1)\n\t\t\t{\n\t\t\t\tj--;\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\tswap(nums[i], nums[j]);\n\t\t}\n\t}\n};",
    "language": "cpp"
  },
  "node_id": "dailycode-826dfcaf541147bb8be5b8a3103697ad",
  "license": "csdn.net",
  "created_at": 1637894158,
  "topic_link": "https://bbs.csdn.net/topics/600470126"
}