@@ -92,3 +92,129 @@ class Solution(object):
9292 """
9393 return all (r == 0 or c == 0 or matrix[r- 1 ][c- 1 ] == val for r, row in enumerate (matrix) for c, val in enumerate (row))
9494```
95+
96+
97+ ## Follow up 1:
98+
99+ ```
100+ What if the matrix is stored on disk, and the memory is limited such that you can only load at most one row of the matrix into the memory at once?
101+ ```
102+
103+ > 思路 1
104+ ****** - 时间复杂度: O(N^2)****** - 空间复杂度: O(N)******
105+
106+ 一行一行取呗,简单
107+
108+ ``` python
109+ class Solution (object ):
110+ def isToeplitzMatrix (self , matrix ):
111+ """
112+ :type matrix: List[List[int]]
113+ :rtype: bool
114+ """
115+ if len (matrix) <= 1 or len (matrix[0 ]) <= 1 :
116+ return True
117+
118+ row, col, queue = len (matrix), len (matrix[0 ]), []
119+
120+ for i in range (col):
121+ queue.append(matrix[0 ][i])
122+
123+ for i in range (1 , row):
124+ queue.pop()
125+ for j in range (1 , col):
126+ if matrix[i][j] == queue[j- 1 ]:
127+ continue
128+ else :
129+ return False
130+ queue.insert(0 , matrix[i][0 ])
131+
132+ return True
133+ ```
134+
135+
136+ ## Follow up 2:
137+
138+ ```
139+ What if the matrix is so large that you can only load up a partial row into the memory at once?
140+ ```
141+
142+
143+ > 思路 1
144+ ****** - 时间复杂度: O(N^2)****** - 空间复杂度: O(N)****** \
145+
146+
147+ 每次只取一行的一部分
148+
149+ ``` python
150+ class Solution (object ):
151+ def isToeplitzMatrix (self , matrix ):
152+ """
153+ :type matrix: List[List[int]]
154+ :rtype: bool
155+ """
156+ row = len (matrix)
157+ col = len (matrix[0 ]) if row else 0
158+ step = 3 # This step indicates the maximum length of 'piece' which can be loaded at one time.
159+ size, idx = 1 , col - 1
160+
161+ while idx >= 0 :
162+ size = min (idx+ 1 , step)
163+ memory = [0 ] * size
164+ for i in range (size):
165+ memory[size- 1 - i] = matrix[0 ][idx- i] # set memory
166+ for j in range (1 , min (row, col)): # check the related pieces of rows
167+ right_bound = min (idx+ j, col- 1 )
168+ left_bound = max (idx- step+ 1 + j, j)
169+ m, n = 0 , left_bound
170+ while m < size and n <= right_bound:
171+ if matrix[j][n] != memory[m]:
172+ print (1 )
173+ return False
174+ m += 1
175+ n += 1
176+ idx -= step
177+
178+ idx = 0
179+ while idx < row: # for the purpose of completeness, the criteria should include two sides of the matrix
180+ size = min (row- 1 - idx, step)
181+ memory = [0 ] * size
182+ for i in range (size):
183+ memory[size- 1 - i] = matrix[row- idx- 1 - i][0 ] # set memory
184+ for j in range (1 , min (row, col)): # check the related pieces of rows
185+ upper_bound = max (row- idx- step+ j, j+ 1 )
186+ lower_bound = min (row- idx- 1 + j, row- 1 )
187+ m, n = 0 , upper_bound
188+ while m < size and n <= lower_bound:
189+ if matrix[n][j] != memory[m]:
190+ print (2 )
191+ return False
192+ m += 1
193+ n += 1
194+ idx += step
195+
196+ return True
197+ ```
198+
199+
200+
201+
202+
203+
204+
205+
206+
207+
208+
209+
210+
211+
212+
213+
214+
215+
216+
217+
218+
219+
220+
0 commit comments