Created
October 31, 2022 12:37
-
-
Save dluciano/efcded6b46dbbd7a8f933966f466fab9 to your computer and use it in GitHub Desktop.
766. Toeplitz Matrix
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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