-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2326-SpiralMatrixIV.go
More file actions
182 lines (167 loc) · 5.33 KB
/
2326-SpiralMatrixIV.go
File metadata and controls
182 lines (167 loc) · 5.33 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
package main
// 2326. Spiral Matrix IV
// 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.
// Example 1:
// <img src="https://assets.leetcode.com/uploads/2022/05/09/ex1new.jpg" />
// Input: m = 3, n = 5, head = [3,0,2,6,8,1,7,9,4,2,5,5,0]
// Output: [[3,0,2,6,8],[5,0,-1,-1,1],[5,2,4,9,7]]
// Explanation: The diagram above shows how the values are printed in the matrix.
// Note that the remaining spaces in the matrix are filled with -1.
// Example 2:
// <img src="https://assets.leetcode.com/uploads/2022/05/11/ex2.jpg" />
// Input: m = 1, n = 4, head = [0,1,2]
// Output: [[0,1,2,-1]]
// Explanation: The diagram above shows how the values are printed from left to right in the matrix.
// The last space in the matrix is set to -1.
// Constraints:
// 1 <= m, n <= 10^5
// 1 <= m * n <= 10^5
// The number of nodes in the list is in the range [1, m * n].
// 0 <= Node.val <= 1000
import "fmt"
type ListNode struct {
Val int
Next *ListNode
}
// 打印链表
func printListNode(l *ListNode) {
if nil == l {
return
}
for {
if nil == l.Next {
fmt.Print(l.Val)
break
} else {
fmt.Print(l.Val, " -> ")
}
l = l.Next
}
fmt.Println()
}
// 数组创建链表
func makeListNode(arr []int) *ListNode {
if (len(arr) == 0) {
return nil
}
l := len(arr) - 1
head := &ListNode{arr[l], nil}
for i := l - 1; i >= 0; i-- {
n := &ListNode{arr[i], head}
head = n
}
return head
}
/**
* Definition for singly-linked list.
* type ListNode struct {
* Val int
* Next *ListNode
* }
*/
func spiralMatrix(m int, n int, head *ListNode) [][]int {
res := make([][]int, m)
for i := 0; i < m; i++ { res[i] = make([]int, n) }
for i := 0; i < m; i++ { // 生成一个全是 -1的 m * n 的矩阵
for j := 0; j < n; j++ {
res[i][j] = -1
}
}
y, x := 0, 0
dirs := [][]int{ {0,0}, {0,1}, {1,0}, {0,-1}, {-1,0}, }
d, shrink, lim := 1, 0, 0
for head != nil { // 遍历链表
res[y][x] = head.Val
head = head.Next
i, j := y + dirs[d][0], x + dirs[d][1]
if i >= shrink && i < m - lim && j >= lim && j < n - lim {
y, x = i, j
} else {
d++
if d == 4 {
shrink++
} else if d > 4 {
d = 1
lim++
}
y, x = y + dirs[d][0], x + dirs[d][1]
}
}
return res
}
func spiralMatrix1(m int, n int, head *ListNode) [][]int {
res := make([][]int, m)
for i := range res {
res[i] = make([]int, n)
for j := range res[i] {
res[i][j] = -1
}
}
up, down, left, right := 0, m - 1, 0, n - 1
cur := head
for cur != nil {
for i := left; i <= right; i++ {
if cur == nil {
return res
}
res[up][i] = cur.Val
cur = cur.Next
}
up++
for i := up; i <= down; i++ {
if cur == nil {
return res
}
res[i][right] = cur.Val
cur = cur.Next
}
right--
for i := right; i >= left; i-- {
if cur == nil {
return res
}
res[down][i] = cur.Val
cur = cur.Next
}
down--
for i := down; i >= up; i-- {
if cur == nil {
return res
}
res[i][left] = cur.Val
cur = cur.Next
}
left++
}
return res
}
func main() {
// Example 1:
// <img src="https://assets.leetcode.com/uploads/2022/05/09/ex1new.jpg" />
// Input: m = 3, n = 5, head = [3,0,2,6,8,1,7,9,4,2,5,5,0]
// Output: [[3,0,2,6,8],[5,0,-1,-1,1],[5,2,4,9,7]]
// Explanation: The diagram above shows how the values are printed in the matrix.
// Note that the remaining spaces in the matrix are filled with -1.
list1 := makeListNode([]int{3,0,2,6,8,1,7,9,4,2,5,5,0})
printListNode(list1) // 3 -> 0 -> 2 -> 6 -> 8 -> 1 -> 7 -> 9 -> 4 -> 2 -> 5 -> 5 -> 0
fmt.Println(spiralMatrix(3, 5, list1)) // [[3,0,2,6,8],[5,0,-1,-1,1],[5,2,4,9,7]]
// Example 2:
// <img src="https://assets.leetcode.com/uploads/2022/05/11/ex2.jpg" />
// Input: m = 1, n = 4, head = [0,1,2]
// Output: [[0,1,2,-1]]
// Explanation: The diagram above shows how the values are printed from left to right in the matrix.
// The last space in the matrix is set to -1.
list2 := makeListNode([]int{0,1,2}) // 0 -> 1 -> 2
printListNode(list2)
fmt.Println(spiralMatrix(1, 4, list2)) // [[0,1,2,-1]]
list11 := makeListNode([]int{3,0,2,6,8,1,7,9,4,2,5,5,0})
printListNode(list11) // 3 -> 0 -> 2 -> 6 -> 8 -> 1 -> 7 -> 9 -> 4 -> 2 -> 5 -> 5 -> 0
fmt.Println(spiralMatrix1(3, 5, list11)) // [[3,0,2,6,8],[5,0,-1,-1,1],[5,2,4,9,7]]
list12 := makeListNode([]int{0,1,2}) // 0 -> 1 -> 2
printListNode(list12)
fmt.Println(spiralMatrix1(1, 4, list12)) // [[0,1,2,-1]]
}