-
Notifications
You must be signed in to change notification settings - Fork 0
/
Octomber_31.cpp
55 lines (45 loc) · 1.11 KB
/
Octomber_31.cpp
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
// 766. Toeplitz Matrix
// https://leetcode.com/problems/toeplitz-matrix/description/
Method 1
class Solution {
public:
int row,col;
bool check(vector<vector<int>>& matrix,int i,int j){
int k = matrix[i][j];
while(i<row and j<col){
if(matrix[i++][j++]!=k){
return false;
}
}
return true;
}
bool isToeplitzMatrix(vector<vector<int>>& matrix) {
row = matrix.size();
col = matrix[0].size();
for(int i=0;i<col;i++){
if(check(matrix,0,i) == false){
return false;
}
}
for(int i=1;i<row;i++){
if(check(matrix,i,0) == false){
return false;
}
}
return true;
}
};
Method 2
class Solution {
public:
bool isToeplitzMatrix(vector<vector<int>>& matrix) {
int row = matrix.size();
int col = matrix[0].size();
for(int i=1;i<row;i++){
for(int j=1;j<col;j++){
if(matrix[i][j]!=matrix[i-1][j-1]) return false;
}
}
return true;
}
};