{"id":4187,"date":"2018-10-13T20:06:20","date_gmt":"2018-10-14T03:06:20","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=4187"},"modified":"2018-10-13T20:06:48","modified_gmt":"2018-10-14T03:06:48","slug":"leetcode-922-sort-array-by-parity-ii","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/uncategorized\/leetcode-922-sort-array-by-parity-ii\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 922. Sort Array By Parity II"},"content":{"rendered":"<h1><strong>Problem<\/strong><\/h1>\n<p>Given an array\u00a0<code>A<\/code>\u00a0of non-negative integers, half of the integers in A are odd, and half of the integers are even.<\/p>\n<p>Sort the array so that whenever\u00a0<code>A[i]<\/code>\u00a0is odd,\u00a0<code>i<\/code>\u00a0is odd; and whenever\u00a0<code>A[i]<\/code>\u00a0is even,\u00a0<code>i<\/code>\u00a0is even.<\/p>\n<p>You may return any answer array that satisfies this condition.<\/p>\n<p><strong>Example 1:<\/strong><\/p>\n<pre class=\"crayon:false \"><strong>Input: <\/strong><span id=\"example-input-1-1\">[4,2,5,7]<\/span>\r\n<strong>Output: <\/strong><span id=\"example-output-1\">[4,5,2,7]<\/span>\r\n<strong>Explanation: <\/strong>[4,7,2,5], [2,5,4,7], [2,7,4,5] would also have been accepted.\r\n<\/pre>\n<p><strong>Note:<\/strong><\/p>\n<ol>\n<li><code>2 &lt;= A.length &lt;= 20000<\/code><\/li>\n<li><code>A.length % 2 == 0<\/code><\/li>\n<li><code>0 &lt;= A[i] &lt;= 1000<\/code><\/li>\n<\/ol>\n<h1><strong>Solution 1: Brute Force<\/strong><\/h1>\n<p>Time complexity: O(n)<\/p>\n<p>Space complexity: O(n)<\/p>\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre class=\"lang:c++ decode:true \">\/\/ Author: Huahua\r\nclass Solution {\r\npublic:\r\n  vector&lt;int&gt; sortArrayByParityII(vector&lt;int&gt;&amp; A) {\r\n    vector&lt;int&gt; evens;\r\n    vector&lt;int&gt; odds;\r\n    for (int a : A)\r\n      if (a % 2 == 0) \r\n        evens.push_back(a);\r\n      else\r\n        odds.push_back(a);\r\n    auto it1 = begin(evens);\r\n    auto it2 = begin(odds);\r\n    for (int i = 0; i &lt; A.size(); ++i) {\r\n      A[i] = *it1++;\r\n      swap(it1, it2);\r\n    }\r\n    return A;\r\n  }\r\n};<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Problem Given an array\u00a0A\u00a0of non-negative integers, half of the integers in A are odd, and half of the integers are even. Sort the array so&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[222,23],"class_list":["post-4187","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-easy","tag-sort","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/4187","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=4187"}],"version-history":[{"count":3,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/4187\/revisions"}],"predecessor-version":[{"id":4191,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/4187\/revisions\/4191"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=4187"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=4187"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=4187"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}