-
Notifications
You must be signed in to change notification settings - Fork 43
/
toeplitz-matrix.py
126 lines (114 loc) · 3.36 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
"""
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.
Example 1:
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.
Example 2:
Input:
matrix = [
[1,2],
[2,2]
]
Output: False
Explanation:
The diagonal "[1, 2]" has different elements.
"""
# V0
class Solution(object):
def isToeplitzMatrix(self, matrix):
m, n = len(matrix), len(matrix[0])
for i in range(m-1):
for j in range(n-1):
if matrix[i][j] != matrix[i+1][j+1]:
return False
return True
# V1
# https://blog.csdn.net/mario_mmh/article/details/79940837
class Solution(object):
def isToeplitzMatrix(self, matrix):
"""
:type matrix: List[List[int]]
:rtype: bool
"""
for i in range(len(matrix)-1):
for j in range(len(matrix[0])-1):
if matrix[i][j] != matrix[i+1][j+1]:
return False
return True
### Test case : dev
# V1'
# http://bookshadow.com/weblog/2018/01/21/leetcode-toeplitz-matrix/
class Solution(object):
def isToeplitzMatrix(self, matrix):
"""
:type matrix: List[List[int]]
:rtype: bool
"""
vmap = collections.defaultdict(set)
M, N = len(matrix), len(matrix[0])
for x in range(M):
for y in range(N):
vmap[y - x].add(matrix[x][y])
if len(vmap[y - x]) > 1: return False
return True
# V1''
# https://leetcode.com/problems/toeplitz-matrix/solution/
# IDEA : GROUP BY CATEGORY
# Time Complexity: O(M*N)
# Space Complexity: O(M+N)
class Solution(object):
def isToeplitzMatrix(self, matrix):
groups = {}
for r, row in enumerate(matrix):
for c, val in enumerate(row):
if r-c not in groups:
groups[r-c] = val
elif groups[r-c] != val:
return False
return True
# V1'''
# https://leetcode.com/problems/toeplitz-matrix/solution/
# IDEA : Compare With Top-Left Neighbor
# Time Complexity: O(M*N)
# Space Complexity: O(1)
class Solution(object):
def isToeplitzMatrix(self, matrix):
return all(r == 0 or c == 0 or matrix[r-1][c-1] == val
for r, row in enumerate(matrix)
for c, val in enumerate(row))
# V2
# Time: O(m * n)
# Space: O(1)
class Solution(object):
def isToeplitzMatrix(self, matrix):
"""
:type matrix: List[List[int]]
:rtype: bool
"""
return all(i == 0 or j == 0 or matrix[i-1][j-1] == val
for i, row in enumerate(matrix)
for j, val in enumerate(row))
class Solution2(object):
def isToeplitzMatrix(self, matrix):
"""
:type matrix: List[List[int]]
:rtype: bool
"""
for row_index, row in enumerate(matrix):
for digit_index, digit in enumerate(row):
if not row_index or not digit_index:
continue
if matrix[row_index - 1][digit_index - 1] != digit:
return False
return True