4646
4747## 解法
4848
49- ### 方法一
49+ ### 方法一:模拟
50+
51+ 我们定义一个二维数组 $\text{ans}$,用来存放链表中的元素,初始时全部填充为 $-1$。定义三个变量 $i, j, k$,分别表示当前的行、列和方向。定义一个数组 $\text{dirs}$,表示四个方向的偏移量。
52+
53+ 然后我们开始遍历链表,每次遍历一个节点,就将当前节点的值填充到 $\text{ans}[ i] [ j ] $ 中,然后更新链表的指针,如果链表为空,说明所有的元素都已经填充完毕,退出循环。
54+
55+ 否则,我们需要找到下一个元素的位置,我们可以通过当前位置 $(i, j)$ 和当前方向 $k$ 来计算下一个位置 $(x, y)$,如果 $(x, y)$ 在矩阵的范围内,并且 $\text{ans}[ x] [ y ] $ 为 $-1$,说明 $(x, y)$ 还没有被填充过,我们就将 $(x, y)$ 作为下一个位置,否则我们需要更换方向。
56+
57+ 遍历完链表之后,我们就得到了一个螺旋矩阵,返回即可。
58+
59+ 时间复杂度 $O(m \times n)$,空间复杂度 $O(m \times n)$。其中 $m$ 和 $n$ 分别表示矩阵的行数和列数。
5060
5161<!-- tabs:start -->
5262
5969class Solution :
6070 def spiralMatrix (self , m : int , n : int , head : Optional[ListNode]) -> List[List[int ]]:
6171 ans = [[- 1 ] * n for _ in range (m)]
62- i = j = p = 0
63- dirs = [[ 0 , 1 ], [ 1 , 0 ], [ 0 , - 1 ], [ - 1 , 0 ]]
72+ i = j = k = 0
73+ dirs = ( 0 , 1 , 0 , - 1 , 0 )
6474 while 1 :
6575 ans[i][j] = head.val
6676 head = head.next
67- if not head:
77+ if head is None :
6878 break
6979 while 1 :
70- x, y = i + dirs[p][0 ], j + dirs[p][1 ]
71- if x < 0 or y < 0 or x >= m or y >= n or ~ ans[x][y]:
72- p = (p + 1 ) % 4
73- else :
80+ x, y = i + dirs[k], j + dirs[k + 1 ]
81+ if 0 <= x < m and 0 <= y < n and ans[x][y] == - 1 :
7482 i, j = x, y
7583 break
84+ k = (k + 1 ) % 4
7685 return ans
7786```
7887
@@ -90,26 +99,25 @@ class Solution:
9099class Solution {
91100 public int [][] spiralMatrix (int m , int n , ListNode head ) {
92101 int [][] ans = new int [m][n];
93- for (int [] row : ans) {
102+ for (var row : ans) {
94103 Arrays . fill(row, - 1 );
95104 }
96- int i = 0 , j = 0 , p = 0 ;
97- int [][] dirs = {{ 0 , 1 }, { 1 , 0 }, { 0 , - 1 }, { - 1 , 0 } };
105+ int i = 0 , j = 0 , k = 0 ;
106+ final int [] dirs = {0 , 1 , 0 , - 1 , 0 };
98107 while (true ) {
99108 ans[i][j] = head. val;
100109 head = head. next;
101110 if (head == null ) {
102111 break ;
103112 }
104113 while (true ) {
105- int x = i + dirs[p][0 ], y = j + dirs[p][1 ];
106- if (x < 0 || y < 0 || x >= m || y >= n || ans[x][y] >= 0 ) {
107- p = (p + 1 ) % 4 ;
108- } else {
114+ int x = i + dirs[k], y = j + dirs[k + 1 ];
115+ if (x >= 0 && x < m && y >= 0 && y < n && ans[x][y] == - 1 ) {
109116 i = x;
110117 j = y;
111118 break ;
112119 }
120+ k = (k + 1 ) % 4 ;
113121 }
114122 }
115123 return ans;
@@ -132,20 +140,22 @@ class Solution {
132140public:
133141 vector<vector<int >> spiralMatrix(int m, int n, ListNode* head) {
134142 vector<vector<int >> ans(m, vector<int >(n, -1));
135- int i = 0, j = 0, p = 0;
136- vector<vector< int >> dirs = {{ 0, 1}, {1, 0}, {0, -1}, {-1, 0} };
143+ int i = 0, j = 0, k = 0;
144+ const int dirs[ 5 ] = {0, 1, 0, -1, 0 };
137145 while (1) {
138146 ans[ i] [ j ] = head->val;
139147 head = head->next;
140- if (!head) break;
148+ if (!head) {
149+ break;
150+ }
141151 while (1) {
142- int x = i + dirs[ p] [ 0 ] , y = j + dirs[ p] [ 1 ] ;
143- if (x < 0 || y < 0 || x >= m || y >= n || ans[ x] [ y ] >= 0)
144- p = (p + 1) % 4;
145- else {
146- i = x, j = y;
152+ int x = i + dirs[ k] , y = j + dirs[ k + 1] ;
153+ if (x >= 0 && x < m && y >= 0 && y < n && ans[ x] [ y ] == -1) {
154+ i = x;
155+ j = y;
147156 break;
148157 }
158+ k = (k + 1) % 4;
149159 }
150160 }
151161 return ans;
@@ -169,22 +179,20 @@ func spiralMatrix(m int, n int, head *ListNode) [][]int {
169179 ans[i][j] = -1
170180 }
171181 }
172- i, j, p := 0, 0, 0
173- dirs := [][] int{{ 0, 1}, {1, 0}, {0, -1}, {-1, 0} }
182+ i, j, k := 0, 0, 0
183+ dirs := [5] int{0, 1, 0, -1, 0 }
174184 for {
175185 ans[i][j] = head.Val
176- head = head.Next
177- if head == nil {
186+ if head = head.Next; head == nil {
178187 break
179188 }
180189 for {
181- x, y := i+dirs[p][0], j+dirs[p][1]
182- if x < 0 || y < 0 || x >= m || y >= n || ans[x][y] >= 0 {
183- p = (p + 1) % 4
184- } else {
190+ x, y := i+dirs[k], j+dirs[k+1]
191+ if x >= 0 && x < m && y >= 0 && y < n && ans[x][y] == -1 {
185192 i, j = x, y
186193 break
187194 }
195+ k = (k + 1) % 4
188196 }
189197 }
190198 return ans
@@ -205,26 +213,24 @@ func spiralMatrix(m int, n int, head *ListNode) [][]int {
205213 */
206214
207215function spiralMatrix(m : number , n : number , head : ListNode | null ): number [][] {
208- const dirs = [
209- [0 , 1 ],
210- [1 , 0 ],
211- [0 , - 1 ],
212- [- 1 , 0 ],
213- ];
214- let ans = Array .from ({ length: m }, v => new Array (n ).fill (- 1 ));
215- let i = 0 ,
216- j = 0 ,
217- k = 0 ;
218- while (head ) {
216+ const ans: number [][] = Array .from ({ length: m }, () => Array (n ).fill (- 1 ));
217+ const dirs: number [] = [0 , 1 , 0 , - 1 , 0 ];
218+ let [i, j, k] = [0 , 0 , 0 ];
219+ while (1 ) {
219220 ans [i ][j ] = head .val ;
220221 head = head .next ;
221- let x = i + dirs [k ][0 ];
222- let y = j + dirs [k ][1 ];
223- if (x < 0 || x > m - 1 || y < 0 || y > n - 1 || ans [x ][y ] != - 1 ) {
222+ if (! head ) {
223+ break ;
224+ }
225+ while (1 ) {
226+ const [x, y] = [i + dirs [k ], j + dirs [k + 1 ]];
227+ if (x >= 0 && x < m && y >= 0 && y < n && ans [x ][y ] === - 1 ) {
228+ i = x ;
229+ j = y ;
230+ break ;
231+ }
224232 k = (k + 1 ) % 4 ;
225233 }
226- i = i + dirs [k ][0 ];
227- j = j + dirs [k ][1 ];
228234 }
229235 return ans ;
230236}
0 commit comments