{"id":6880,"date":"2020-06-06T22:23:19","date_gmt":"2020-06-07T05:23:19","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=6880"},"modified":"2020-06-06T22:23:38","modified_gmt":"2020-06-07T05:23:38","slug":"leetcode-1470-shuffle-the-array","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/simulation\/leetcode-1470-shuffle-the-array\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1470. Shuffle the Array"},"content":{"rendered":"\n<p>Given the array&nbsp;<code>nums<\/code>&nbsp;consisting of&nbsp;<code>2n<\/code>&nbsp;elements in the form&nbsp;<code>[x<sub>1<\/sub>,x<sub>2<\/sub>,...,x<sub>n<\/sub>,y<sub>1<\/sub>,y<sub>2<\/sub>,...,y<sub>n<\/sub>]<\/code>.<\/p>\n\n\n\n<p><em>Return the array in the form<\/em>&nbsp;<code>[x<sub>1<\/sub>,y<sub>1<\/sub>,x<sub>2<\/sub>,y<sub>2<\/sub>,...,x<sub>n<\/sub>,y<sub>n<\/sub>]<\/code>.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> nums = [2,5,1,3,4,7], n = 3\n<strong>Output:<\/strong> [2,3,5,4,1,7] \n<strong>Explanation:<\/strong> Since x<sub>1<\/sub>=2, x<sub>2<\/sub>=5, x<sub>3<\/sub>=1, y<sub>1<\/sub>=3, y<sub>2<\/sub>=4, y<sub>3<\/sub>=7 then the answer is [2,3,5,4,1,7].\n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> nums = [1,2,3,4,4,3,2,1], n = 4\n<strong>Output:<\/strong> [1,4,2,3,3,2,4,1]\n<\/pre>\n\n\n\n<p><strong>Example 3:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> nums = [1,1,2,2], n = 2\n<strong>Output:<\/strong> [1,2,1,2]\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;= n &lt;= 500<\/code><\/li><li><code>nums.length == 2n<\/code><\/li><li><code>1 &lt;= nums[i] &lt;= 10^3<\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Simulation<\/strong><\/h2>\n\n\n\n<p>Time complexity: O(n)<br>Space complexity: O(n)<\/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  vector<int> shuffle(vector<int>& nums, int n) {\n    vector<int> ans(n * 2);\n    for (int i = 0; i < 2 * n; ++i)\n      ans[i] = nums[i \/ 2 + n * (i % 2)];\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Given the array&nbsp;nums&nbsp;consisting of&nbsp;2n&nbsp;elements in the form&nbsp;[x1,x2,&#8230;,xn,y1,y2,&#8230;,yn]. Return the array in the form&nbsp;[x1,y1,x2,y2,&#8230;,xn,yn]. Example 1: Input: nums = [2,5,1,3,4,7], n = 3 Output: [2,3,5,4,1,7] Explanation:&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[48],"tags":[20,222,179],"class_list":["post-6880","post","type-post","status-publish","format-standard","hentry","category-simulation","tag-array","tag-easy","tag-simulation","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6880","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=6880"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6880\/revisions"}],"predecessor-version":[{"id":6882,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6880\/revisions\/6882"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=6880"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=6880"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=6880"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}