-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathsolution.java
More file actions
76 lines (57 loc) · 2 KB
/
Copy pathsolution.java
File metadata and controls
76 lines (57 loc) · 2 KB
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
class Solution {
public int[][] rotateGrid(int[][] grid, int k) {
int m = grid.length;
int n = grid[0].length;
// Total layers
int layers = Math.min(m, n) / 2;
for (int layer = 0; layer < layers; layer++) {
ArrayList<Integer> nums = new ArrayList<>();
int top = layer;
int bottom = m - layer - 1;
int left = layer;
int right = n - layer - 1;
// Store top row
for (int j = left; j <= right; j++) {
nums.add(grid[top][j]);
}
// Store right column
for (int i = top + 1; i <= bottom - 1; i++) {
nums.add(grid[i][right]);
}
// Store bottom row
for (int j = right; j >= left; j--) {
nums.add(grid[bottom][j]);
}
// Store left column
for (int i = bottom - 1; i >= top + 1; i--) {
nums.add(grid[i][left]);
}
int len = nums.size();
// Effective rotations
int rotate = k % len;
int[] rotated = new int[len];
// Left rotation
for (int i = 0; i < len; i++) {
rotated[i] = nums.get((i + rotate) % len);
}
int idx = 0;
// Fill top row
for (int j = left; j <= right; j++) {
grid[top][j] = rotated[idx++];
}
// Fill right column
for (int i = top + 1; i <= bottom - 1; i++) {
grid[i][right] = rotated[idx++];
}
// Fill bottom row
for (int j = right; j >= left; j--) {
grid[bottom][j] = rotated[idx++];
}
// Fill left column
for (int i = bottom - 1; i >= top + 1; i--) {
grid[i][left] = rotated[idx++];
}
}
return grid;
}
}