LeetCode solutions

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

Read the problem on LeetCode View on GitHub

Python

Accepted on LeetCode — runtime 6ms, memory 18MB, accepted 2026-01-01.

python
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

Source