LeetCode 1929: Concatenation of Array (Index Mapping to 2n Output)
LeetCode 1929ArraySimulationToday we solve LeetCode 1929 - Concatenation of Array.
Source: https://leetcode.com/problems/concatenation-of-array/
English
Problem Summary
Given an integer array nums of length n, return an array ans of length 2n where ans[i] = nums[i] and ans[i + n] = nums[i] for every valid index i.
Key Insight
This is a direct construction task. For each element in nums, write it twice into the output: once in the first half and once in the second half.
Algorithm Steps
1) Let n = nums.length, create ans with size 2n.
2) Loop i from 0 to n - 1.
3) Set ans[i] = nums[i] and ans[i + n] = nums[i].
4) Return ans.
Complexity Analysis
Time: O(n).
Space: O(n) extra for output array of size 2n.
Common Pitfalls
- Allocating size n instead of 2n.
- Using i + n without checking index bounds due to wrong size.
- Overusing list append operations when direct index write is clearer.
Reference Implementations (Java / Go / C++ / Python / JavaScript)
class Solution {
public int[] getConcatenation(int[] nums) {
int n = nums.length;
int[] ans = new int[2 * n];
for (int i = 0; i < n; i++) {
ans[i] = nums[i];
ans[i + n] = nums[i];
}
return ans;
}
}func getConcatenation(nums []int) []int {
n := len(nums)
ans := make([]int, 2*n)
for i := 0; i < n; i++ {
ans[i] = nums[i]
ans[i+n] = nums[i]
}
return ans
}class Solution {
public:
vector getConcatenation(vector& nums) {
int n = (int)nums.size();
vector ans(2 * n);
for (int i = 0; i < n; i++) {
ans[i] = nums[i];
ans[i + n] = nums[i];
}
return ans;
}
}; class Solution:
def getConcatenation(self, nums: List[int]) -> List[int]:
n = len(nums)
ans = [0] * (2 * n)
for i in range(n):
ans[i] = nums[i]
ans[i + n] = nums[i]
return ans/**
* @param {number[]} nums
* @return {number[]}
*/
var getConcatenation = function(nums) {
const n = nums.length;
const ans = new Array(2 * n);
for (let i = 0; i < n; i++) {
ans[i] = nums[i];
ans[i + n] = nums[i];
}
return ans;
};中文
题目概述
给你一个长度为 n 的整数数组 nums,需要构造长度为 2n 的数组 ans,满足对每个下标 i 都有:
ans[i] = nums[i],ans[i + n] = nums[i]。
核心思路
这是一个直接构造题:每个元素在结果数组中写两次,分别写到前半段和后半段。
算法步骤
1)令 n = nums.length,创建长度为 2n 的数组 ans。
2)遍历下标 i = 0..n-1。
3)执行 ans[i] = nums[i] 和 ans[i + n] = nums[i]。
4)返回 ans。
复杂度分析
时间复杂度:O(n)。
空间复杂度:O(n)(输出数组长度为 2n)。
常见陷阱
- 结果数组只开成 n,导致越界。
- i + n 的写入位置计算错误。
- 明明可直接按索引写入,却用复杂拼接导致可读性变差。
多语言参考实现(Java / Go / C++ / Python / JavaScript)
class Solution {
public int[] getConcatenation(int[] nums) {
int n = nums.length;
int[] ans = new int[2 * n];
for (int i = 0; i < n; i++) {
ans[i] = nums[i];
ans[i + n] = nums[i];
}
return ans;
}
}func getConcatenation(nums []int) []int {
n := len(nums)
ans := make([]int, 2*n)
for i := 0; i < n; i++ {
ans[i] = nums[i]
ans[i+n] = nums[i]
}
return ans
}class Solution {
public:
vector getConcatenation(vector& nums) {
int n = (int)nums.size();
vector ans(2 * n);
for (int i = 0; i < n; i++) {
ans[i] = nums[i];
ans[i + n] = nums[i];
}
return ans;
}
}; class Solution:
def getConcatenation(self, nums: List[int]) -> List[int]:
n = len(nums)
ans = [0] * (2 * n)
for i in range(n):
ans[i] = nums[i]
ans[i + n] = nums[i]
return ans/**
* @param {number[]} nums
* @return {number[]}
*/
var getConcatenation = function(nums) {
const n = nums.length;
const ans = new Array(2 * n);
for (let i = 0; i < n; i++) {
ans[i] = nums[i];
ans[i + n] = nums[i];
}
return ans;
};
Comments