diff options
Diffstat (limited to 'store/works/solutions/leetcode/cpp/766.cpp')
| -rw-r--r-- | store/works/solutions/leetcode/cpp/766.cpp | 44 | 
1 files changed, 44 insertions, 0 deletions
diff --git a/store/works/solutions/leetcode/cpp/766.cpp b/store/works/solutions/leetcode/cpp/766.cpp new file mode 100644 index 0000000..3e8a015 --- /dev/null +++ b/store/works/solutions/leetcode/cpp/766.cpp @@ -0,0 +1,44 @@ +#include <algorithm>
 +#include <vector>
 +
 +using std::vector;
 +
 +class Solution {
 +public:
 +  bool isToeplitzMatrix(vector<vector<int>> &matrix) {
 +    int row_count = matrix.size();
 +    int col_count = matrix.front().size();
 +
 +    if (matrix.size() == 1)
 +      return true;
 +    if (matrix.front().size() == 1)
 +      return true;
 +
 +    // offset = col - row
 +    // max(offset) = row_count - 2
 +    // min(offset) = -(col_count - 2)
 +    for (int offset = -(col_count - 2); offset <= (row_count - 2); offset++) {
 +      int row, col;
 +      if (offset >= 0) {
 +        row = offset;
 +        col = 0;
 +      } else {
 +        row = 0;
 +        col = -offset;
 +      }
 +
 +      int value = matrix[row][col];
 +      row++;
 +      col++;
 +
 +      while (row < row_count && col < col_count) {
 +        if (matrix[row][col] != value) {
 +          return false;
 +        }
 +        row++;
 +        col++;
 +      }
 +    }
 +    return true;
 +  }
 +};
  | 
