Skip to content

Commit d770861

Browse files
committed
Toeplitz Matrix
1 parent a194bb5 commit d770861

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

Toeplitz_Matrix.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# A matrix is Toeplitz if every diagonal from top-left to bottom-right has the same element.
2+
#
3+
# Now given an M x N matrix, return True if and only if the matrix is Toeplitz.
4+
#
5+
# Example 1:
6+
#
7+
# Input:
8+
# matrix = [
9+
# [1,2,3,4],
10+
# [5,1,2,3],
11+
# [9,5,1,2]
12+
# ]
13+
# Output: True
14+
# Explanation:
15+
# In the above grid, the diagonals are:
16+
# "[9]", "[5, 5]", "[1, 1, 1]", "[2, 2, 2]", "[3, 3]", "[4]".
17+
# In each diagonal all elements are the same, so the answer is True
18+
#
19+
#
20+
# Example 2:
21+
#
22+
# Input:
23+
# matrix = [
24+
# [1,2],
25+
# [2,2]
26+
# ]
27+
# Output: False
28+
# Explanation:
29+
# The diagonal "[1, 2]" has different elements.
30+
31+
32+
class Solution:
33+
def isToeplitzMatrix(self, matrix):
34+
35+
for r in range(len(matrix) - 1):
36+
for c in range(len(matrix[0]) - 1):
37+
if matrix[r][c] != matrix[r + 1][c + 1]:
38+
return False
39+
return True

0 commit comments

Comments
 (0)