xxxxxxxxxx
function arrayRotate(arr, reverse) {
if (reverse) arr.unshift(arr.pop());
else arr.push(arr.shift());
return arr;
}
xxxxxxxxxx
function rotate(matrix) {
return matrix[0].map((col, i) => matrix.map((row) => row[i]))
}
function rotateAntiClockwise(matrix) {
return matrix[0].map((col, i) => matrix.map((row) => row[i]).reverse())
}
xxxxxxxxxx
public static int[] Rotate(int[] arr , int k)
{
k %= arr.Length;
var ls = new int[arr.Length];
for (int i = 0; i < ls.Length-k; i++)
{
ls[i+k] = arr[i];
}
var j = ls.Length-k;
for (int i = 0; i < k; i++)
{
ls[i] = arr[j++];
}
return ls;
}
xxxxxxxxxx
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]