forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.ts
40 lines (40 loc) · 1.16 KB
/
Solution.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
function rotateGrid(grid: number[][], k: number): number[][] {
const m = grid.length;
const n = grid[0].length;
const rotate = (p: number, k: number) => {
const nums: number[] = [];
for (let j = p; j < n - p - 1; ++j) {
nums.push(grid[p][j]);
}
for (let i = p; i < m - p - 1; ++i) {
nums.push(grid[i][n - p - 1]);
}
for (let j = n - p - 1; j > p; --j) {
nums.push(grid[m - p - 1][j]);
}
for (let i = m - p - 1; i > p; --i) {
nums.push(grid[i][p]);
}
const l = nums.length;
k %= l;
if (k === 0) {
return;
}
for (let j = p; j < n - p - 1; ++j) {
grid[p][j] = nums[k++ % l];
}
for (let i = p; i < m - p - 1; ++i) {
grid[i][n - p - 1] = nums[k++ % l];
}
for (let j = n - p - 1; j > p; --j) {
grid[m - p - 1][j] = nums[k++ % l];
}
for (let i = m - p - 1; i > p; --i) {
grid[i][p] = nums[k++ % l];
}
};
for (let p = 0; p < Math.min(m, n) >> 1; ++p) {
rotate(p, k);
}
return grid;
}