-
Notifications
You must be signed in to change notification settings - Fork 0
/
**L_2326.java**
50 lines (49 loc) · 1.44 KB
/
**L_2326.java**
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
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
class Solution {
public int[][] spiralMatrix(int m, int n, ListNode head) {
int[][] res = new int[m][n];
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
res[i][j] = -1;
}
}
int top = 0;
int bottom = m-1;
int left = 0;
int right = n-1;
ListNode temp = head;
int j = 0;
while(top <= bottom && left <= right && temp != null){
for (int i = left; i <= right && temp != null; i++) {
res[top][i] = temp.val;
temp = temp.next;
}
top++;
for (int i = top; i <= bottom && temp != null; i++) {
res[i][right] = temp.val;
temp = temp.next;
}
right--;
for (int i = right; i >= left && temp != null; i--) {
res[bottom][i] = temp.val;
temp = temp.next;
}
bottom--;
for (int i = bottom; i >= top && temp != null; i--) {
res[i][left] = temp.val;
temp = temp.next;
}
left++;
}
return res;
}
}