forked from liuyubobobo/Play-Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
46 lines (35 loc) · 1.16 KB
/
main.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
/// Source : https://leetcode.com/problems/toeplitz-matrix/description/
/// Author : liuyubobobo
/// Time : 2018-01-26
#include <iostream>
#include <vector>
#include <unordered_map>
using namespace std;
/// Using HashTable to memorize the r-c value
/// Time Complexity: O(N^2)
/// Space Complexity: O(N)
class Solution {
public:
bool isToeplitzMatrix(vector<vector<int>>& matrix) {
unordered_map<int, int> table;
for(int i = 0 ; i < matrix.size() ; i ++)
for(int j = 0 ; j < matrix[i].size() ; j ++){
unordered_map<int, int>::iterator iter = table.find(i - j);
if(iter == table.end())
table.insert(make_pair(i - j, matrix[i][j]));
else if(iter->second != matrix[i][j])
return false;
}
return true;
}
};
void printBool(bool res){
cout << (res ? "True" : "False") << endl;
}
int main() {
vector<vector<int>> matrix1 = {{1,2,3,4}, {5,1,2,3}, {9,5,1,2}};
printBool(Solution().isToeplitzMatrix(matrix1));
vector<vector<int>> matrix2 = {{1, 2}, {2, 2}};
printBool(Solution().isToeplitzMatrix(matrix2));
return 0;
}