{"id":707,"date":"2020-06-23T22:33:42","date_gmt":"2020-06-23T14:33:42","guid":{"rendered":"http:\/\/www.guanhaobo.cn\/?p=707"},"modified":"2020-06-23T22:33:42","modified_gmt":"2020-06-23T14:33:42","slug":"jz13-%e8%b0%83%e6%95%b4%e6%95%b0%e7%bb%84%e9%a1%ba%e5%ba%8f%e4%bd%bf%e5%a5%87%e6%95%b0%e4%bd%8d%e4%ba%8e%e5%81%b6%e6%95%b0%e5%89%8d%e9%9d%a2","status":"publish","type":"post","link":"https:\/\/www.guanhaobo.cn\/?p=707","title":{"rendered":"JZ13 \u2014 \u8c03\u6574\u6570\u7ec4\u987a\u5e8f\u4f7f\u5947\u6570\u4f4d\u4e8e\u5076\u6570\u524d\u9762"},"content":{"rendered":"<h3>\u9898\u76ee\u63cf\u8ff0<\/h3>\n<p>\u8f93\u5165\u4e00\u4e2a\u6574\u6570\u6570\u7ec4\uff0c\u5b9e\u73b0\u4e00\u4e2a\u51fd\u6570\u6765\u8c03\u6574\u8be5\u6570\u7ec4\u4e2d\u6570\u5b57\u7684\u987a\u5e8f\uff0c\u4f7f\u5f97\u6240\u6709\u7684\u5947\u6570\u4f4d\u4e8e\u6570\u7ec4\u7684\u524d\u534a\u90e8\u5206\uff0c\u6240\u6709\u7684\u5076\u6570\u4f4d\u4e8e\u6570\u7ec4\u7684\u540e\u534a\u90e8\u5206\uff0c\u5e76\u4fdd\u8bc1\u5947\u6570\u548c\u5947\u6570\uff0c\u5076\u6570\u548c\u5076\u6570\u4e4b\u95f4\u7684\u76f8\u5bf9\u4f4d\u7f6e\u4e0d\u53d8\u3002<\/p>\n<h3>\u9898\u76ee\u5206\u6790<\/h3>\n<p>\u5982\u679c\u53ef\u4ee5\u5f00\u8f9f\u65b0\u7684\u6570\u7ec4\uff0c\u975e\u5e38\u7b80\u5355\u3002<br \/>\n\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-cpp line-numbers\">class Solution\n{\npublic:\n    void reOrderArray(vector&lt;int&gt; &amp;array)\n    {\n        vector&lt;int&gt; odd;  \/\/\u5947\u6570\n        vector&lt;int&gt; even; \/\/\u5076\u6570\n        for (int i = 0; i &lt; array.size(); i++)\n        {\n            if (array[i] % 2)\n                odd.push_back(array[i]);\n            else\n                even.push_back(array[i]);\n        }\n        array.clear();\n        for (int i = 0; i &lt; odd.size(); i++)\n            array.push_back(odd[i]);\n        for (int i = 0; i &lt; even.size(); i++)\n            array.push_back(even[i]);\n    }\n};\n<\/code><\/pre>\n<p>\u5982\u679c\u4e0d\u53ef\u4ee5\u5f00\u8f9f\u65b0\u7684\u6570\u7ec4\uff0c\u53ef\u4ee5\u4f7f\u7528\u5982\u4e0b\u7b56\u7565\uff1a<br \/>\n\u4f7f\u7528\u53d8\u91cf<code>index<\/code>\u8bb0\u5f55\u63d2\u5165\u4f4d\u7f6e\uff0c\u521d\u59cb\u503c\u4e3a0.\u904d\u5386\u539f\u6570\u7ec4\uff0c\u5982\u679c\u4e3a\u5947\u6570\uff0c\u5219\u5c06<code>[index, i-1]<\/code>\u7684\u6570\u636e\u90fd\u5411\u53f3\u79fb\u52a8\u4e00\u4e2a\u5355\u4f4d\u3002\u7136\u540e\u5c06\u6570\u636e\u63d2\u5165\u5230\u4f4d\u7f6e<code>index<\/code>\u4e2d\u3002\u4ee4<code>index++<\/code><\/p>\n<pre><code class=\"language-cpp line-numbers\">class Solution\n{\npublic:\n    void reOrderArray(vector&lt;int&gt; &amp;array)\n    {\n        int temp, index = 0;\n        for (int i = 0; i &lt; array.size(); i++)\n        {\n            if (array[i] % 2)\n            {\n                temp = array[i];\n                for (int j = i; j &gt; index; j--)\n                    array[j] = array[j - 1];\n                array[index++] = temp;\n            }\n        }\n    }\n};\n<\/code><\/pre>\n<p>\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a<code>O(n^2)<\/code><\/p>\n<h4>By The Way<\/h4>\n<p>\u5224\u65ad\u5947\u5076\u6027\u9664\u4e86\u53ef\u4ee5\u7528<code>n % 2<\/code>\uff0c\u8fd8\u53ef\u4ee5\u7528<code>n &amp; 1<\/code><\/p>\n<h3>Java<\/h3>\n<pre><code class=\"language-java line-numbers\">public class Solution {\n    public void reOrderArray(int[] array) {\n        int index = 0, temp;\n        for (int i = 0; i &lt; array.length; i++) {\n            if ((array[i] &amp; 1) == 1) {\n                temp = array[i];\n                for (int j = i; j &gt; index; j--)\n                    array[j] = array[j - 1];\n                array[index++] = temp;\n            }\n        }\n    }\n}\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u9898\u76ee\u63cf\u8ff0 \u8f93\u5165\u4e00\u4e2a\u6574\u6570\u6570\u7ec4\uff0c\u5b9e\u73b0\u4e00\u4e2a\u51fd\u6570\u6765\u8c03\u6574\u8be5\u6570\u7ec4\u4e2d\u6570\u5b57\u7684\u987a\u5e8f\uff0c\u4f7f\u5f97\u6240\u6709\u7684\u5947\u6570\u4f4d\u4e8e\u6570\u7ec4\u7684\u524d\u534a\u90e8\u5206\uff0c\u6240\u6709\u7684\u5076\u6570 [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[39],"class_list":["post-707","post","type-post","status-publish","format-standard","hentry","category-algo","tag-offer"],"_links":{"self":[{"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=\/wp\/v2\/posts\/707","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=707"}],"version-history":[{"count":0,"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=\/wp\/v2\/posts\/707\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=707"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=707"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=707"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}