Skip to content

1929. Concatenation of Array 👍

  • Time: $O(n)$
  • Space: $O(n)$
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
class Solution {
 public:
  vector<int> getConcatenation(vector<int>& nums) {
    const int n = nums.size();

    for (int i = 0; i < n; ++i)
      nums.push_back(nums[i]);

    return nums;
  }
};
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
class Solution {
  public int[] getConcatenation(int[] nums) {
    final int n = nums.length;

    int[] ans = new int[n * 2];

    for (int i = 0; i < n; ++i)
      ans[i] = ans[i + n] = nums[i];

    return ans;
  }
}
1
2
3
class Solution:
  def getConcatenation(self, nums: list[int]) -> list[int]:
    return nums * 2