|
| 1 | +<p>You are given an integer array <code>nums</code> of length <code>n</code>.</p> |
| 2 | + |
| 3 | +<p>Construct a new array <code>ans</code> of length <code>2 * n</code> such that the first <code>n</code> elements are the same as <code>nums</code>, and the next <code>n</code> elements are the elements of <code>nums</code> in reverse order.</p> |
| 4 | + |
| 5 | +<p>Formally, for <code>0 <= i <= n - 1</code>:</p> |
| 6 | + |
| 7 | +<ul> |
| 8 | + <li><code>ans[i] = nums[i]</code></li> |
| 9 | + <li><code>ans[i + n] = nums[n - i - 1]</code></li> |
| 10 | +</ul> |
| 11 | + |
| 12 | +<p>Return an integer array <code>ans</code>.</p> |
| 13 | + |
| 14 | +<p> </p> |
| 15 | +<p><strong class="example">Example 1:</strong></p> |
| 16 | + |
| 17 | +<div class="example-block"> |
| 18 | +<p><strong>Input:</strong> <span class="example-io">nums = [1,2,3]</span></p> |
| 19 | + |
| 20 | +<p><strong>Output:</strong> <span class="example-io">[1,2,3,3,2,1]</span></p> |
| 21 | + |
| 22 | +<p><strong>Explanation:</strong></p> |
| 23 | + |
| 24 | +<p>The first <code>n</code> elements of <code>ans</code> are the same as <code>nums</code>.</p> |
| 25 | + |
| 26 | +<p>For the next <code>n = 3</code> elements, each element is taken from <code>nums</code> in reverse order:</p> |
| 27 | + |
| 28 | +<ul> |
| 29 | + <li><code>ans[3] = nums[2] = 3</code></li> |
| 30 | + <li><code>ans[4] = nums[1] = 2</code></li> |
| 31 | + <li><code>ans[5] = nums[0] = 1</code></li> |
| 32 | +</ul> |
| 33 | + |
| 34 | +<p>Thus, <code>ans = [1, 2, 3, 3, 2, 1]</code>.</p> |
| 35 | +</div> |
| 36 | + |
| 37 | +<p><strong class="example">Example 2:</strong></p> |
| 38 | + |
| 39 | +<div class="example-block"> |
| 40 | +<p><strong>Input:</strong> <span class="example-io">nums = [1]</span></p> |
| 41 | + |
| 42 | +<p><strong>Output:</strong> <span class="example-io">[1,1]</span></p> |
| 43 | + |
| 44 | +<p><strong>Explanation:</strong></p> |
| 45 | + |
| 46 | +<p>The array remains the same when reversed. Thus, <code>ans = [1, 1]</code>.</p> |
| 47 | +</div> |
| 48 | + |
| 49 | +<p> </p> |
| 50 | +<p><strong>Constraints:</strong></p> |
| 51 | + |
| 52 | +<ul> |
| 53 | + <li><code>1 <= nums.length <= 100</code></li> |
| 54 | + <li><code>1 <= nums[i] <= 100</code></li> |
| 55 | +</ul> |
0 commit comments