难度: Easy
原题连接
内容描述
A matrix is Toeplitz if every diagonal from top-left to bottom-right has the same element.
Now given an M x N matrix, return True if and only if the matrix is Toeplitz.
Example 1:
Input:
matrix = [
[1,2,3,4],
[5,1,2,3],
[9,5,1,2]
]
Output: True
Explanation:
In the above grid, the diagonals are:
"[9]", "[5, 5]", "[1, 1, 1]", "[2, 2, 2]", "[3, 3]", "[4]".
In each diagonal all elements are the same, so the answer is True.
Example 2:
Input:
matrix = [
[1,2],
[2,2]
]
Output: False
Explanation:
The diagonal "[1, 2]" has different elements.
Note:
matrix will be a 2D array of integers.
matrix will have a number of rows and columns in range [1, 20].
matrix[i][j] will be integers in range [0, 99].
Follow up:
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?
What if the matrix is so large that you can only load up a partial row into the memory at once?
思路 1 - 时间复杂度: O(N^2)- 空间复杂度: O(1)******
遍历每个元素,如果它与其右下角元素不相等,那么返回False
beats 100%
class Solution(object):
def isToeplitzMatrix(self, matrix):
"""
:type matrix: List[List[int]]
:rtype: bool
"""
if not matrix or len(matrix) == 0:
return True
row = len(matrix)
col = len(matrix[0]) if row else 0
for i in range(row):
for j in range(col):
if i + 1 <= row - 1 and j + 1 <= col - 1 and matrix[i][j] != matrix[i+1][j+1]:
return False
return True
1行版本
class Solution(object):
def isToeplitzMatrix(self, matrix):
"""
:type matrix: List[List[int]]
:rtype: bool
"""
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))
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?
思路 1 - 时间复杂度: O(N^2)- 空间复杂度: O(N)******
一行一行取呗,简单
class Solution(object):
def isToeplitzMatrix(self, matrix):
"""
:type matrix: List[List[int]]
:rtype: bool
"""
if len(matrix) <= 1 or len(matrix[0]) <= 1:
return True
row, col, queue = len(matrix), len(matrix[0]), []
for i in range(col):
queue.append(matrix[0][i])
for i in range(1, row):
queue.pop()
for j in range(1, col):
if matrix[i][j] == queue[j-1]:
continue
else:
return False
queue.insert(0, matrix[i][0])
return True
What if the matrix is so large that you can only load up a partial row into the memory at once?
思路 1 - 时间复杂度: O(N^2)- 空间复杂度: O(N)******
每次只取一行的一部分, 参考leavedelta的解法
class Solution(object):
def isToeplitzMatrix(self, matrix):
"""
:type matrix: List[List[int]]
:rtype: bool
"""
row = len(matrix)
col = len(matrix[0]) if row else 0
step = 3 # This step indicates the maximum length of 'piece' which can be loaded at one time.
size, idx = 1, col - 1
while idx >= 0:
size = min(idx+1, step)
memory = [0] *size
for i in range(size):
memory[size-1-i] = matrix[0][idx-i] # set memory
for j in range(1, min(row, col)): # check the related pieces of rows
right_bound = min(idx+j, col-1)
left_bound = max(idx-step+1+j, j)
m, n = 0, left_bound
while m < size and n <= right_bound:
if matrix[j][n] != memory[m]:
print(1)
return False
m += 1
n += 1
idx -= step
idx = 0
while idx < row: # for the purpose of completeness, the criteria should include two sides of the matrix
size = min(row-1-idx, step)
memory = [0] *size
for i in range(size):
memory[size-1-i] = matrix[row-idx-1-i][0] # set memory
for j in range(1, min(row, col)): # check the related pieces of rows
upper_bound = max(row-idx-step+j, j+1)
lower_bound = min(row-idx-1+j, row-1)
m, n = 0, upper_bound
while m < size and n <= lower_bound:
if matrix[n][j] != memory[m]:
print(2)
return False
m += 1
n += 1
idx += step
return True