[
array
]
BinarySearch 0127 Swap Consecutive Index Pairs
Problem statement
https://binarysearch.com/problems/Swap-Consecutive-Index-Pairs/
Solution
Just do what is asked in loop.
Complexity
It is O(n)
for time and O(1)
for additional space.
Code
class Solution:
def solve(self, nums):
n = len(nums)
for i in range(n//4 + 1):
if 4*i + 2 < n:
nums[4*i], nums[4*i+2] = nums[4*i+2], nums[4*i]
if 4*i + 3 < n:
nums[4*i+1], nums[4*i+3] = nums[4*i+3], nums[4*i+1]
return nums