From dc1f0c4c0096013799416664894c5194dc7e1f52 Mon Sep 17 00:00:00 2001 From: Yuqian Yang Date: Fri, 28 Feb 2025 23:13:39 +0800 Subject: chore(store): move everything to store. --- store/works/solutions/leetcode/cpp/766.cpp | 44 ++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 store/works/solutions/leetcode/cpp/766.cpp (limited to 'store/works/solutions/leetcode/cpp/766.cpp') 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 +#include + +using std::vector; + +class Solution { +public: + bool isToeplitzMatrix(vector> &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; + } +}; -- cgit v1.2.3