-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Expand file tree
/
Copy pathspiral-matrix-iv.py
More file actions
28 lines (25 loc) · 828 Bytes
/
spiral-matrix-iv.py
File metadata and controls
28 lines (25 loc) · 828 Bytes
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
# Time: O(m * n)
# Space: O(1)
# Definition for singly-linked list.
class ListNode(object):
def __init__(self, val=0, next=None):
pass
# linked list, array
class Solution(object):
def spiralMatrix(self, m, n, head):
"""
:type m: int
:type n: int
:type head: Optional[ListNode]
:rtype: List[List[int]]
"""
directions = [(0, 1), (1, 0), (0, -1), (-1, 0)]
result = [[-1]*n for _ in xrange(m)]
i = j = d = 0
while head:
result[i][j] = head.val
if not (0 <= i+directions[d][0] < m and 0 <= j+directions[d][1] < n and result[i+directions[d][0]][j+directions[d][1]] == -1):
d = (d+1)%4
i, j = i+directions[d][0], j+directions[d][1]
head = head.next
return result