-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtoeplitz-matrix.py
62 lines (46 loc) · 1.58 KB
/
toeplitz-matrix.py
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
'''766. Toeplitz Matrix
https://leetcode.com/problems/toeplitz-matrix/
Given an m x n matrix, return true if the matrix is Toeplitz. Otherwise, return false.
A matrix is Toeplitz if every diagonal from top-left to bottom-right has the same elements.
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.
matrix = [[1,2,3,4],
[5,1,2,3],
[9,5,1,2],
[7,9,5,1],
[6,7,9,5],
[8,6,7,9]]
'''
class Solution:
def isToeplitzMatrix(self, matrix: list[list[int]]) -> bool:
m = len(matrix)
n = len(matrix[0])
if m == n == 1: return True
# T_flag = True
# print("m=",m,"n=",n)
for i in range(-m,n):
flag = True
# if T_flag == False: break
for el in zip(matrix):
if 0 <= i < n:
# print(el[0][i])
if flag:
check = el[0][i]
flag = False
elif check != el[0][i]:
return False
break
elif i >= n: break
i += 1
# print('next loop')
return True
matrix = [[1,2,3,4],
[6,7,9,5],
[8,6,7,9]]
s = Solution()
test = s.isToeplitzMatrix(matrix)
print(test)