-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtoe.py
More file actions
75 lines (61 loc) · 2.15 KB
/
toe.py
File metadata and controls
75 lines (61 loc) · 2.15 KB
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
63
64
65
66
67
68
69
70
71
72
73
74
75
class Solution(object):
def isToeplitzMatrix(self, matrix):
"""
:type matrix: List[List[int]]
:rtype: bool
"""
# toeplitz rule and example
"""
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.
Input: matrix = [[1,2,3,4],[5,1,2,3],[9,5,1,2]]
Output: True
Explanation:
1234
5123
9512
In the above grid, the diagonals are "[9]", "[5, 5]", "[1, 1, 1]", "[2, 2, 2]", "[3, 3]", "[4]", and in each diagonal all elements are the same, so the answer is True.
"""
# neglect the single element occuring at the beginning and the last
# Diagnals with size 2, 3... are to be considered
# Based on n x n make the checks, get the last list and for each number go back to the previous list and previous element to check the diagnal
n = len(matrix)
select = matrix[n-1]
s = len(select)
curr = n-1
i = 1
if s <= 1:
return True
while i > 0:
element = matrix[curr][i]
j = i-1
k = curr-1
if k < 0:
i = -1
while j >= 0 and k >= 0:
#print j, k
#print matrix[k][j], element
if matrix[k][j] != element:
return False
k -= 1
j -= 1
i += 1
if i == s:
i = s-1
#j = i-1
curr = curr - 1
#k = curr-1
#element = matrix[k][i]
"""
for i in range(1,s):
element = select[i]
j = i-1
k = n-2
while j >= 0 and k >= 0:
print matrix[k][j], element
if matrix[k][j] != element:
return False
k -= 1
j -= 1
"""
return True