-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspiral_mat_iv.go
41 lines (36 loc) · 1.13 KB
/
spiral_mat_iv.go
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
package problem2326
import . "leetcodedaily/helpers/listnode"
/*
You are given two integers m and n, which represent the dimensions of a matrix.
You are also given the head of a linked list of integers.
Generate an m x n matrix that contains the integers in the linked list presented in spiral order (clockwise), starting from the top-left of the matrix.
If there are remaining empty spaces, fill them with -1.
Return the generated matrix.
*/
var dirs = [4][2]int{
{0, 1}, {1, 0}, {0, -1}, {-1, 0},
}
func spiralMatrix(m int, n int, head *ListNode) [][]int {
var res = make([][]int, m)
var row, col, dir int
for i := range res {
res[i] = make([]int, n)
for j := range res[i] {
res[i][j] = -1
}
}
for i := 0; head != nil && i < m*n; i++ {
res[row][col] = head.Val
head = head.Next
nrow, ncol := row+dirs[dir][0], col+dirs[dir][1]
if outOfBounds(res, nrow, ncol) || res[nrow][ncol] != -1 {
dir = (dir + 1) % 4
nrow, ncol = row+dirs[dir][0], col+dirs[dir][1]
}
row, col = nrow, ncol
}
return res
}
func outOfBounds(mat [][]int, row, col int) bool {
return row < 0 || col < 0 || row >= len(mat) || col >= len(mat[0])
}