Algorithm
Problem Name: 189. Rotate Array
Given an integer array nums
, rotate the array to the right by k
steps, where k
is non-negative.
Example 1:
Input: nums = [1,2,3,4,5,6,7], k = 3 Output: [5,6,7,1,2,3,4] Explanation: rotate 1 steps to the right: [7,1,2,3,4,5,6] rotate 2 steps to the right: [6,7,1,2,3,4,5] rotate 3 steps to the right: [5,6,7,1,2,3,4]
Example 2:
Input: nums = [-1,-100,3,99], k = 2 Output: [3,99,-1,-100] Explanation: rotate 1 steps to the right: [99,-1,-100,3] rotate 2 steps to the right: [3,99,-1,-100]
Constraints:
1 <= nums.length <= 105
-231 <= nums[i] <= 231 - 1
0 <= k <= 105
Code Examples
#1 Code Example with C Programming
Code -
C Programming
#include <stdio.h>
void rotate(int nums[], int n, int k) {
int i, j, c;
i = j = 0;
int pre = nums[i];
int t;
for(c = 0; c < n; c++) {
i = (i + k) % n;
t = nums[i];
nums[i] = pre;
pre = t;
if (i == j) {
i = ++j;
pre = nums[i];
}
}
}
void transpose(int *matrix, int m, int n) {
int i, j, c;
i = j = 0;
int pre = matrix[0];
int t;
for (c = 0; c < m * n - 1; c++) { /* pay attention it's m*n-1 */
i = (i % n) * m + (i / n);
t = matrix[i];
matrix[i] = pre;
pre = t;
if (i == j) {
i = ++j;
pre = matrix[i];
}
}
}
void print(int nums[], int n) {
int i;
for (i = 0; i < n; i++) {
printf("%d ", nums[i]);
}
printf("\n");
}
int main()
{
int a[7] = {1, 2, 3, 4, 5, 6, 7};
int n = 7;
int k = 4;
rotate(a, n, k);
print(a, n);
int b[6] = {1, 2, 3, 4, 5, 6};
n = 6;
k = 3;
rotate(b, n, k);
print(b, n);
return 0;
}
Copy The Code &
Try With Live Editor
Input
Output
#2 Code Example with Java Programming
Code -
Java Programming
class Solution {
public void rotate(int[] nums, int k) {
int n = nums.length;
k %= n;
int count = 0;
for (int i = 0; count < n; i++) {
int currIdx = i;
int prevValue = nums[i];
do {
int nextIdx = (currIdx + k) % n;
int tempValue = nums[nextIdx];
nums[nextIdx] = prevValue;
prevValue = tempValue;
currIdx = nextIdx;
count++;
} while (i != currIdx);
}
}
}
Copy The Code &
Try With Live Editor
Input
Output
#3 Code Example with Javascript Programming
Code -
Javascript Programming
const rotate = function(nums, k) {
nums.unshift(...nums.splice(nums.length - k))
};
Copy The Code &
Try With Live Editor
Input
Output
#4 Code Example with Python Programming
Code -
Python Programming
class Solution:
def rotate(self, nums, k):
"""
:type nums: List[int]
:type k: int
:rtype: void Do not return anything, modify nums in-place instead.
"""
n=k%len(nums)
nums[:] = nums[-n:] + nums[:-n]
Copy The Code &
Try With Live Editor
Input
Output
#5 Code Example with C# Programming
Code -
C# Programming
namespace LeetCode
{
public class _0189_RotateArray
{
public void Rotate(int[] nums, int k)
{
if (k >= nums.Length)
k %= nums.Length;
for (int start = 0, count = 0; start < k && count < nums.Length; start++)
{
int curr = start;
var prev = nums[curr];
do
{
var next = curr + k;
if (next >= nums.Length)
next -= nums.Length;
var temp = nums[next];
nums[next] = prev;
prev = temp;
curr = next;
count++;
} while (start != curr);
}
}
}
}
Copy The Code &
Try With Live Editor
Input
Output