Skip to content

Commit 80880e3

Browse files
Sync LeetCode submission Runtime - 0 ms (100.00%), Memory - 30.2 MB (78.76%)
1 parent 996ba3f commit 80880e3

2 files changed

Lines changed: 65 additions & 0 deletions

File tree

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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 &lt;= i &lt;= 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>&nbsp;</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>&nbsp;</p>
50+
<p><strong>Constraints:</strong></p>
51+
52+
<ul>
53+
<li><code>1 &lt;= nums.length &lt;= 100</code></li>
54+
<li><code>1 &lt;= nums[i] &lt;= 100</code></li>
55+
</ul>
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
class Solution {
2+
public:
3+
vector<int> concatWithReverse(vector<int>& nums) {
4+
int n = nums.size();
5+
vector<int> ans(2*n);
6+
for (int i=0; i<n; i++) ans[i] = nums[i];
7+
for (int i=n; i<2*n; i++) ans[i] = nums[n - (i - n) - 1];
8+
return ans;
9+
}
10+
};

0 commit comments

Comments
 (0)