Skip to content

Commit 3f7f66d

Browse files
solves #2319: Check if Matrix Is X-Matrix in java
1 parent f7decc8 commit 3f7f66d

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -744,9 +744,9 @@
744744
| 2300 | [Successful Pairs of Spells and Potions](https://leetcode.com/problems/successful-pairs-of-spells-and-potions) | [![Python](assets/python.png)](python/successfull_pairs_of_spells_and_potions.py) | |
745745
| 2303 | [Calculate Amount Paid in Taxes](https://leetcode.com/problems/calculate-amount-paid-in-taxes) | [![Java](assets/java.png)](src/CalculateAmountPaidInTaxes.java) | |
746746
| 2309 | [Greatest English Letter in Upper and Lower Case](https://leetcode.com/problems/greatest-english-letter-in-upper-and-lower-case) | [![Java](assets/java.png)](src/GreatestEnglishLetterInUpperAndLowerCase.java) | |
747-
| 2315 | [Count Asterisks](https://leetcode.com/problems/count-asterisks) | [![Java](assets/java.png)](src/CountAsterisks.java) [![Python](assets/python.png)](python/successfull_pairs_of_spells_and_potions.py) | |
747+
| 2315 | [Count Asterisks](https://leetcode.com/problems/count-asterisks) | [![Java](assets/java.png)](src/CountAsterisks.java) [![Python](assets/python.png)](python/successfull_pairs_of_spells_and_potions.py) | |
748748
| 2316 | [Count Unreachable Pairs of Nodes in an Undirected Graph](https://leetcode.com/problems/count-unreachable-pairs-of-nodes-in-an-undirected-graph) | [![Python](assets/python.png)](python/count_unreachable_pair_of_node_in_an_undirectable_graph.py) | |
749-
| 2319 | [Check if Matrix Is X-Matrix](https://leetcode.com/problems/check-if-matrix-is-x-matrix) | [![Python](assets/python.png)](python/count_unreachable_pair_of_node_in_an_undirectable_graph.py) | |
749+
| 2319 | [Check if Matrix Is X-Matrix](https://leetcode.com/problems/check-if-matrix-is-x-matrix) | [![Java](assets/java.png)](src/CountAsterisks.java) | |
750750
| 2325 | [Decode the Message](https://leetcode.com/problems/decode-the-message) | [![Python](assets/python.png)](python/count_unreachable_pair_of_node_in_an_undirectable_graph.py) | |
751751
| 2331 | [Evaluate Boolean Binary Tree](https://leetcode.com/problems/evaluate-boolean-binary-tree) | [![Python](assets/python.png)](python/count_unreachable_pair_of_node_in_an_undirectable_graph.py) | |
752752
| 2335 | [Minimum Amount of Time to Fill Cups](https://leetcode.com/problems/minimum-amount-of-time-to-fill-cups) | [![Python](assets/python.png)](python/count_unreachable_pair_of_node_in_an_undirectable_graph.py) | |

src/CheckIfMatrixIsXMatrix.java

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// https://leetcode.com/problems/check-if-matrix-is-x-matrix
2+
// T: O(n * n)
3+
// S: O(1)
4+
5+
public class CheckIfMatrixIsXMatrix {
6+
public boolean checkXMatrix(int[][] grid) {
7+
for (int row = 0 ; row < grid.length ; row++) {
8+
for (int column = 0 ; column < grid.length ; column++) {
9+
if (isDiagonalCell(row, column, grid.length)) {
10+
if (grid[row][column] == 0) return false;
11+
} else if (grid[row][column] != 0) return false;
12+
}
13+
}
14+
return true;
15+
}
16+
17+
private boolean isDiagonalCell(int row, int column, int length) {
18+
return row == column || column == length - row - 1;
19+
}
20+
}

0 commit comments

Comments
 (0)