{"id":5677,"date":"2019-10-01T00:16:00","date_gmt":"2019-10-01T07:16:00","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=5677"},"modified":"2019-10-01T00:26:22","modified_gmt":"2019-10-01T07:26:22","slug":"leetcode-75-sort-colors","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/algorithms\/array\/leetcode-75-sort-colors\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 75. Sort Colors"},"content":{"rendered":"\n<p>Given an array with&nbsp;<em>n<\/em>&nbsp;objects colored red, white or blue, sort them&nbsp;<strong><a href=\"https:\/\/en.wikipedia.org\/wiki\/In-place_algorithm\" target=\"_blank\" rel=\"noreferrer noopener\">in-place<\/a>&nbsp;<\/strong>so that objects of the same color are adjacent, with the colors in the order red, white and blue.<\/p>\n\n\n\n<p>Here, we will use the integers 0, 1, and 2 to represent the color red, white, and blue respectively.<\/p>\n\n\n\n<p><strong>Note:<\/strong>&nbsp;You are not suppose to use the library&#8217;s sort function for this problem.<\/p>\n\n\n\n<p><strong>Example:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> [2,0,2,1,1,0]\n<strong>Output:<\/strong> [0,0,1,1,2,2]<\/pre>\n\n\n\n<p><strong>Follow up:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>A rather straight forward solution is a two-pass algorithm using counting sort.<br>First, iterate the array counting number of 0&#8217;s, 1&#8217;s, and 2&#8217;s, then overwrite array with total number of 0&#8217;s, then 1&#8217;s and followed by 2&#8217;s.<\/li><li>Could you come up with a&nbsp;one-pass algorithm using only constant space?<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution 1: Counting sort<\/strong><\/h2>\n\n\n\n<p>Time complexity: O(n)<br>Space complexity: O(1)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"C++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  void sortColors(vector<int>& nums) {\n    vector<int> c(3);\n    for (int x : nums)\n      ++c[x];\n    auto it = begin(nums);\n    for (int i = 0; i < 3; ++i)\n      while (c[i]--)\n        *it++ = i;\n  }\n};\n<\/pre>\n<\/div><\/div>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution 2: Two pointers<\/strong><\/h2>\n\n\n\n<p>Time complexity: O(n)<br>Space complexity: O(1)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"C++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  void sortColors(vector<int>& nums) {\n    const int n = nums.size();\n    int l = 0;\n    int r = n - 1;    \n    for (int i = 0; i < n; ++i) {\n      if (nums[i] == 0) {\n        swap(nums[i], nums[l++]);\n      } else if (nums[i] == 2 &#038;&#038; i < r) {\n        swap(nums[i--], nums[r--]);\n      }\n    }\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Given an array with&nbsp;n&nbsp;objects colored red, white or blue, sort them&nbsp;in-place&nbsp;so that objects of the same color are adjacent, with the colors in the order&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[184],"tags":[20,177,15],"class_list":["post-5677","post","type-post","status-publish","format-standard","hentry","category-array","tag-array","tag-medium","tag-sorting","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5677","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=5677"}],"version-history":[{"count":4,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5677\/revisions"}],"predecessor-version":[{"id":5682,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5677\/revisions\/5682"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=5677"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=5677"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=5677"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}