Given an array of integers nums, rotate it in-place by one position to its right.
Do not allocate extra space for another array. You must do this by modifying directly the input array with O(1) auxiliary space.
Example 1:
Input: nums = [1, 2, 3, 4, 5] Output: [5, 1, 2, 3, 4]
Example 2:
Input: nums = [4, -2, 13, 1] Output: [1, 4, -2, 13]
Note:
Your algorithm should run in O(n) time and use O(1) auxiliary space.
Walkthroughs of the solution. Try the steps first, then watch to compare approaches.
Free plan: 30 tutor messages. Premium is unlimited.
Saved
Ctrl + Enter to check