2398. Check if Matrix Is X-Matrix
My accepted Python solution to LeetCode problem 2398, Check if Matrix Is X-Matrix, running in 6ms.
- Difficulty: Easy
- Python
- Runtime 6ms
- Memory 18MB
- Updated
Read the problem on LeetCode View on GitHub
The problem statement is LeetCode’s and stays on their site. What follows is my accepted solution.
Python
Accepted on LeetCode — runtime 6ms, memory 18MB, accepted 2026-01-01.
class Solution:
def checkXMatrix(self, grid: List[List[int]]) -> bool:
n = len(grid)
for i in range(n):
for j in range(n):
on_diagonal = (i == j or i + j == n - 1)
if on_diagonal and grid[i][j] == 0:
return False
if not on_diagonal and grid[i][j] != 0:
return False
return True