Skip to content

Commit 27a744f

Browse files
LIGANG YELIGANG YE
authored andcommitted
added Toeplitz Matrix
1 parent 5c09b25 commit 27a744f

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

round_1.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4398,3 +4398,41 @@ def hamming_distance(x, y):
43984398
hamming_distance_y = 4
43994399
print('\n#461. Hamming Distance:')
44004400
print(hamming_distance(hamming_distance_x, hamming_distance_y))
4401+
4402+
4403+
# 766. Toeplitz Matrix
4404+
def is_toeplitz_matrix(matrix):
4405+
if matrix is None:
4406+
return True
4407+
row = len(matrix)
4408+
if row <= 1:
4409+
return True
4410+
col = len(matrix[0])
4411+
if col <= 1:
4412+
return True
4413+
# from 40 to 30 to 00
4414+
for i in range(row - 1, -1, -1):
4415+
j = 0
4416+
new_i = i
4417+
value = matrix[new_i][j]
4418+
while new_i < row and j < col:
4419+
if matrix[new_i][j] != value:
4420+
return False
4421+
new_i += 1
4422+
j += 1
4423+
# from 00 to 01 to 03
4424+
for j in range(0, col):
4425+
i = 0
4426+
new_j = j
4427+
value = matrix[i][new_j]
4428+
while i < row and new_j < col:
4429+
if matrix[i][new_j] != value:
4430+
return False
4431+
i += 1
4432+
new_j += 1
4433+
return True
4434+
4435+
4436+
is_toeplitz_matrix_matrix = [[1, 2, 3, 4], [5, 1, 2, 3], [9, 5, 1, 2]]
4437+
print('\n#766. Toeplitz Matrix:')
4438+
print(is_toeplitz_matrix(is_toeplitz_matrix_matrix))

0 commit comments

Comments
 (0)