Swap Odd and Even Elements
EasyP3Coding
Swap each adjacent pair of elements: the element at index 0 with index 1, index 2 with index 3, and so on. If the length is odd the last element stays where it is.
Return the resulting array.
Example 1
- Input
- nums = [1,2,3,4]
- Output
- [2,1,4,3]
Example 2
- Input
- nums = [1,2,3,4,5]
- Output
- [2,1,4,3,5]
Example 3
- Input
- nums = []
- Output
- []
Constraints
- 0 <= nums.length <= 10^5
- -10^9 <= nums[i] <= 10^9
Hints
Give it a real attempt first. Run shows which examples fail.
Saved automatically
Run checks the 3 visible tests, Submit all 10. ⌘Enter runs, ⌘⇧Enter submits.