forked from lzl124631x/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paths1.cpp
More file actions
21 lines (21 loc) · 682 Bytes
/
s1.cpp
File metadata and controls
21 lines (21 loc) · 682 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// OJ: https://leetcode.com/problems/toeplitz-matrix
// Author: github.com/lzl124631x
// Time: O(MN)
// Space: O(1)
class Solution {
public:
bool isToeplitzMatrix(vector<vector<int>>& matrix) {
int M = matrix.size(), N = matrix[0].size();
for (int i = 0; i < M; ++i) {
for (int x = i + 1, y = 1; x < M && y < N; ++x, ++y) {
if (matrix[x][y] != matrix[x - 1][y - 1]) return false;
}
}
for (int i = 1; i < N; ++i) {
for (int x = 1, y = i + 1; x < M && y < N; ++x, ++y) {
if (matrix[x][y] != matrix[x - 1][y - 1]) return false;
}
}
return true;
}
};