Given an integer array nums of length n, you want to create an array ans of length 2n where ans[i] == nums[i] and ans[i + n] == nums[i] for 0 <= i < n (0-indexed).
Specifically, ans is the concatenation of two nums arrays.
Input: nums =[1,2,1]Output: [1,2,1,1,2,1]Explanation: The array ans is formed as follows:- ans =[nums[0],nums[1],nums[2],nums[0],nums[1],nums[2]]- ans =[1,2,1,1,2,1]
Input: nums =[1,3,2,1]Output: [1,3,2,1,1,3,2,1]Explanation: The array ans is formed as follows:- ans =[nums[0],nums[1],nums[2],nums[3],nums[0],nums[1],nums[2],nums[3]]- ans =[1,3,2,1,1,3,2,1]
classSolution {
publicint[]getConcatenation(int[] nums) {
int n = nums.length;
int[] ans =newint[2 * n];
for (int i = 0; i < n; i++) {
ans[i]= nums[i];
ans[i + n]= nums[i];
}
return ans;
}
}
1
2
3
4
5
6
7
8
9
10
11
classSolution {
fungetConcatenation(nums: IntArray): IntArray {
val n = nums.size
val ans = IntArray(2 * n)
for (i in0 until n) {
ans[i] = nums[i]
ans[i + n] = nums[i]
}
return ans
}
}
1
2
3
4
5
6
7
8
classSolution:
defgetConcatenation(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
1
2
3
4
5
6
7
8
9
10
11
impl Solution {
pubfnget_concatenation(nums: Vec<i32>) -> Vec<i32> {
let n = nums.len();
letmut ans =vec![0; 2* n];
for i in0..n {
ans[i] = nums[i];
ans[i + n] = nums[i];
}
ans
}
}