Skip to content

Instantly share code, notes, and snippets.

@dluciano
Created October 31, 2022 12:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dluciano/efcded6b46dbbd7a8f933966f466fab9 to your computer and use it in GitHub Desktop.
Save dluciano/efcded6b46dbbd7a8f933966f466fab9 to your computer and use it in GitHub Desktop.
766. Toeplitz Matrix
public class Solution {
public bool IsToeplitzMatrix(int[][] matrix) {
var ROWS = matrix.Length;
var COLS = matrix[0].Length;
for(var col = 0; col < COLS; ++col)
if(!IsEqualDiagonal(0, col, matrix))
return false;
for(var row = 0; row < ROWS; ++row)
if(!IsEqualDiagonal(row, 0, matrix))
return false;
return true;
}
static bool IsEqualDiagonal(in int row, in int col, in int[][] matrix){
var ROWS = matrix.Length;
var COLS = matrix[0].Length;
var cur = matrix[row][col];
for(int dRow = row, dCol = col; dRow < ROWS && dCol < COLS; ++dRow, ++dCol){
if(matrix[dRow][dCol] != cur)
return false;
}
return true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment