-
Notifications
You must be signed in to change notification settings - Fork 0
/
2326. Spiral Matrix IV
53 lines (48 loc) · 1.67 KB
/
2326. Spiral Matrix IV
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
class Solution {
public int[][] spiralMatrix(int m, int n, ListNode head) {
int[][] matrix = new int[m][n];
// Initialize the matrix with -1
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
matrix[i][j] = -1;
}
}
int top = 0;
int down = m - 1;
int left = 0;
int right = n - 1;
int id = 0; // Direction identifier (0: left->right, 1: top->down, 2: right->left, 3: down->top)
while (top <= down && left <= right && head != null) {
if (id == 0) { // left to right
for (int i = left; i <= right && head != null; i++) {
matrix[top][i] = head.val;
head = head.next;
}
top++;
}
else if (id == 1) { // top to down
for (int i = top; i <= down && head != null; i++) {
matrix[i][right] = head.val;
head = head.next;
}
right--;
}
else if (id == 2) { // right to left
for (int i = right; i >= left && head != null; i--) {
matrix[down][i] = head.val;
head = head.next;
}
down--;
}
else if (id == 3) { // down to top
for (int i = down; i >= top && head != null; i--) {
matrix[i][left] = head.val;
head = head.next;
}
left++;
}
id = (id + 1) % 4; // Cycle through directions
}
return matrix;
}
}