Commit 4b1af0ab by ligang

'第七次作业'

parent 3beb3747
#!/usr/bin/env python
#-*- coding:utf-8 - *-
#托普利茨矩阵
#如果一个矩阵的每一方向由左上到右下的对角线上具有相同元素,那么这个矩阵是托普利茨矩阵。
# 给定一个 M x N 的矩阵,当且仅当它是托普利茨矩阵时返回 True。
# 示例 1:输入: matrix = [[1,2,3,4], [5,1,2,3], [9,5,1,2]]
# 输出: True
# m*n矩阵
#注意
#说明: matrix 是一个包含整数的二维数组。
# matrix 的行数和列数均在 [1, 20]范围内。
# matrix[i][j] 包含的整数在 [0, 99]范围内。进阶:
class Solution:
def isToeplitzMatrix(self, matrix):
# m行
m = len(matrix)
# n列
n = len(matrix[0])
for i in range(m):
for j in range(n):
if matrix[i][j] != matrix[i+1][j+1]:
return False
return True
matrix = [[1,2,3,4], [5,1,2,3], [9,5,1,2]]
solu = Solution()
solu.isToeplitzMatrix(matrix)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment