Reverse an Array

Easy~10 min

Given an array of integers nums, return the array in reverse order.

For example, given [1, 2, 3, 4, 5], return [5, 4, 3, 2, 1].

You may reverse in-place or return a new array.

Examples

Example 1
Input: nums = [1, 2, 3, 4, 5]
Output: [5, 4, 3, 2, 1]
Explanation: The elements are returned in the opposite order.
Example 2
Input: nums = [10, 20]
Output: [20, 10]
Explanation: Two elements simply swap positions.
Example 3
Input: nums = [7]
Output: [7]
Explanation: A single-element array is already reversed.

Constraints

  • 0 <= nums.length <= 10^5
  • -10^9 <= nums[i] <= 10^9
Code
Ctrl+EnterRun|Ctrl+⇧+EnterSubmit
Output

Run your code to see results

Use Cmd+Enter to run