-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCheck if Matrix is X Matrix.java
35 lines (30 loc) · 1.2 KB
/
Check if Matrix is X Matrix.java
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
// https://leetcode.com/problems/check-if-matrix-is-x-matrix/
/*
Time Complexity: O(n^2) where n = size of grid because we're checking every single element inside the matrix.
Space Complexity: O(1) because no additional dynamic memory was created.
*/
class Solution {
public boolean checkXMatrix(int[][] grid) {
// Iterate grid
for (int row = 0; row < grid.length; row++) {
for (int col = 0; col < grid.length; col++) {
int curNumber = grid[row][col];
// Condition is true if iterator is on a diagonal
// IMPORTANT: grid.length - col - 1 works when checking if iterator is on the right-to-left diagonal.
// Draw a grid with coordinates written out and test out the formula.
if (row == col || row == grid.length - col - 1) {
if (curNumber == 0) {
return false;
}
}
// Condition is met if iterator isn't on a diagonal
else {
if (curNumber != 0) {
return false;
}
}
}
}
return true;
}
}