4. Mirror the Line-up
A choir stands in a single row on stage, and each singer holds a card with a number on it. For the encore the whole row turns around, so the last singer in the row becomes the first, the second-to-last becomes the second, and so on.
Write a method reverseArray that takes the array nums of card numbers, listed from the first singer to the last, and returns a new array with the card numbers in the reversed order.
Example 1
- Input:
- nums = [1,2,3,4,5]
- Output:
- [5,4,3,2,1]
- Explanation:
The row turns around, so the numbers read 5, 4, 3, 2, 1.
Example 2
- Input:
- nums = [10,-3,7]
- Output:
- [7,-3,10]
- Explanation:
The first and last cards swap places; the middle card stays where it is.
Constraints
1 ≤ nums.length ≤ 1000
-1000 ≤ nums[i] ≤ 1000
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)