-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path766_Toeplitz_Matrix.cpp
More file actions
35 lines (34 loc) 路 1.22 KB
/
Copy path766_Toeplitz_Matrix.cpp
File metadata and controls
35 lines (34 loc) 路 1.22 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
/*
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.
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,3,4],[5,1,2,3],[9,5,1,2]]
Output: true
Explanation:
Constraints:
m == matrix.length
n == matrix[i].length
1 <= m, n <= 20
0 <= matrix[i][j] <= 99Follow up:
What if the matrix is stored on disk, and the memory is limited such that you can only load at most one row of the matrix into the memory at once?
What if the matrix is so large that you can only load up a partial row into the memory at once?
*/
class Solution {
public:
bool isToeplitzMatrix(vector<vector<int>>& matrix) {
int m = matrix.size();
int n = matrix[0].size();
for (int i = 0; i < m - 1; ++i) {
for (int j = 0; j < n - 1; ++j) {
if (matrix[i][j] != matrix[i + 1][j + 1])
return false;
}
}
return true;
}
};