{"id":9290,"date":"2021-12-30T22:07:37","date_gmt":"2021-12-31T06:07:37","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=9290"},"modified":"2021-12-30T22:21:56","modified_gmt":"2021-12-31T06:21:56","slug":"leetcode-1929-concatenation-of-array","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/algorithms\/array\/leetcode-1929-concatenation-of-array\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1929. Concatenation of Array"},"content":{"rendered":"\n<p>Given an integer array&nbsp;<code>nums<\/code>&nbsp;of length&nbsp;<code>n<\/code>, you want to create an array&nbsp;<code>ans<\/code>&nbsp;of length&nbsp;<code>2n<\/code>&nbsp;where&nbsp;<code>ans[i] == nums[i]<\/code>&nbsp;and&nbsp;<code>ans[i + n] == nums[i]<\/code>&nbsp;for&nbsp;<code>0 &lt;= i &lt; n<\/code>&nbsp;(<strong>0-indexed<\/strong>).<\/p>\n\n\n\n<p>Specifically,&nbsp;<code>ans<\/code>&nbsp;is the&nbsp;<strong>concatenation<\/strong>&nbsp;of two&nbsp;<code>nums<\/code>&nbsp;arrays.<\/p>\n\n\n\n<p>Return&nbsp;<em>the array&nbsp;<\/em><code>ans<\/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 = [1,2,1]\n<strong>Output:<\/strong> [1,2,1,1,2,1]\n<strong>Explanation:<\/strong> The array ans is formed as follows:\n- ans = [nums[0],nums[1],nums[2],nums[0],nums[1],nums[2]]\n- ans = [1,2,1,1,2,1]<\/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,3,2,1]\n<strong>Output:<\/strong> [1,3,2,1,1,3,2,1]\n<strong>Explanation:<\/strong> The array ans is formed as follows:\n- ans = [nums[0],nums[1],nums[2],nums[3],nums[0],nums[1],nums[2],nums[3]]\n- ans = [1,3,2,1,1,3,2,1]\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>n == nums.length<\/code><\/li><li><code>1 &lt;= n &lt;= 1000<\/code><\/li><li><code>1 &lt;= nums[i] &lt;= 1000<\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Pre-allocation<\/strong><\/h2>\n\n\n\n<p>Pre-allocate an array of length 2 * n.<br>ans[i] = nums[i % n]<\/p>\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> getConcatenation(vector<int>& nums) {\n    const int n = nums.size();\n    vector<int> ans(2 * n);\n    for (int i = 0; i < 2 * n; ++i)\n      ans[i] = nums[i % n];\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Given an integer array&nbsp;nums&nbsp;of length&nbsp;n, you want to create an array&nbsp;ans&nbsp;of length&nbsp;2n&nbsp;where&nbsp;ans[i] == nums[i]&nbsp;and&nbsp;ans[i + n] == nums[i]&nbsp;for&nbsp;0 &lt;= i &lt; n&nbsp;(0-indexed). Specifically,&nbsp;ans&nbsp;is the&nbsp;concatenation&nbsp;of two&nbsp;nums&nbsp;arrays.&#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,222],"class_list":["post-9290","post","type-post","status-publish","format-standard","hentry","category-array","tag-array","tag-easy","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9290","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=9290"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9290\/revisions"}],"predecessor-version":[{"id":9292,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9290\/revisions\/9292"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=9290"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=9290"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=9290"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}