{"id":3984,"date":"2018-09-15T20:32:00","date_gmt":"2018-09-16T03:32:00","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=3984"},"modified":"2018-09-15T20:32:14","modified_gmt":"2018-09-16T03:32:14","slug":"leetcode-905-sort-array-by-parity","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/algorithms\/array\/leetcode-905-sort-array-by-parity\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 905. Sort Array By Parity"},"content":{"rendered":"<h1>Problem<\/h1>\n<p>Given an array\u00a0<code>A<\/code>\u00a0of non-negative integers, return an array consisting of all the even elements of\u00a0<code>A<\/code>, followed by all the odd elements of\u00a0<code>A<\/code>.<\/p>\n<p>You may return any answer array that satisfies this condition.<\/p>\n<div>\n<p><strong>Example 1:<\/strong><\/p>\n<pre class=\"\"><strong>Input: <\/strong><span id=\"example-input-1-1\">[3,1,2,4]<\/span>\r\n<strong>Output: <\/strong><span id=\"example-output-1\">[2,4,3,1]<\/span>\r\nThe outputs [4,2,3,1], [2,4,1,3], and [4,2,1,3] would also be accepted.\r\n<\/pre>\n<p><strong>Note:<\/strong><\/p>\n<ol>\n<li><code>1 &lt;= A.length &lt;= 5000<\/code><\/li>\n<li><code>0 &lt;= A[i] &lt;= 5000<\/code><\/li>\n<\/ol>\n<h1><strong>Solution 1: Split Odd\/Even<\/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, 44 ms\r\nclass Solution {\r\npublic:\r\n  vector&lt;int&gt; sortArrayByParity(vector&lt;int&gt;&amp; A) {\r\n    vector&lt;int&gt; even;\r\n    vector&lt;int&gt; odd;\r\n    for (int a : A) {\r\n      if (a % 2) odd.push_back(a);\r\n      else even.push_back(a);\r\n    }\r\n    even.insert(end(even), begin(odd), end(odd));\r\n    return even;\r\n  }\r\n};<\/pre>\n<\/div><\/div>\n<\/div>\n<h1><strong>Solution 2: Stable sort by key % 2<\/strong><\/h1>\n<p>Time complexity: O(nlogn)<\/p>\n<p>Space complexity: O(1) in-place<\/p>\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre class=\"lang:default decode:true \">\/\/ Author: Huahua, 52 ms\r\nclass Solution {\r\npublic:\r\n  vector&lt;int&gt; sortArrayByParity(vector&lt;int&gt;&amp; A) {\r\n    stable_sort(begin(A), end(A), [](int a, int b){\r\n      return a % 2 &lt; b % 2;\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, return an array consisting of all the even elements of\u00a0A, followed by all the odd elements of\u00a0A. You may&#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":[222,409,408,23],"class_list":["post-3984","post","type-post","status-publish","format-standard","hentry","category-array","tag-easy","tag-even","tag-odd","tag-sort","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/3984","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=3984"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/3984\/revisions"}],"predecessor-version":[{"id":3986,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/3984\/revisions\/3986"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=3984"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=3984"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=3984"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}