37. Play It Twice
A music player is set to loop a playlist exactly two times. The playlist is stored as an array of song IDs. When the loop is on, the whole list plays from beginning to end, and then plays again from the beginning.
Write a method repeatTwice that takes the array nums of song IDs and returns a new array that is nums followed by a second copy of nums, so the result has twice the original length.
Example 1
- Input:
- nums = [1,2,1]
- Output:
- [1,2,1,1,2,1]
- Explanation:
The playlist plays through once and then again: [1, 2, 1, 1, 2, 1].
Example 2
- Input:
- nums = [7]
- Output:
- [7,7]
- Explanation:
A one-song playlist simply plays twice: [7, 7].
Constraints
1 ≤ nums.length ≤ 1000
-109 ≤ nums[i] ≤ 109
How this problem is judged
- Answers
- Your answer must match exactly. Numbers compare by value, so 2 and 2.0 are equal.
Expected complexity
- Time
- O(n)
- Space
- O(n)