Introduction
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.
Return the array ans.
Example
|
|
Process
This was a pretty simple one other that figuring out how to use malloc
again. This would be much simpler to do in python.
After creating an array with size dynamically allocated to 2n
where n
is the size of the input array, I’ve managd to follow the description and concatenate the array using a for loop.
There should probably be a more elegant and efficient way to do this, but I’ll still need to learn more about pointers and arrays.
Code
|
|
{: file="Concatenation of array.c” }
Afterthoughts
-
I initially thought of getting the size “dynamically” by using
sizeof(nums)/sizeof(int)
but I instead realize that I should just use the input parameternumsSize
instead. However, I’m leaving it in the code as a comment for future reference. -
I also had trouble with the second perimeter of the for loop, I used
i+2*numsSize
but I realized that I should just usenumsSize
instead as I didn’t really have to loop through an empty array towards the end. So, looping through the array once withi<numsSize
is enough to insert the values in positioni
andi+numsSize
. -
I didn’t know that I should use all the input parameters and had a heap buffer overflow error until I added the
*returnSize = 2 * numsSize;
line as it is used to return the size of the array.
I’m still pretty new to the leetcode ecosystem and have to do more practices to truly understand the proper way of attempting the questions.