#2319
Check if Matrix Is X-Matrix
pupil · 320 · lc easy +21 · verified · 66.4% accepted · 547 likes · top 72%
Description
A square matrix is an X-Matrix when:
- All diagonal elements are non-zero.
- All off-diagonal elements are 0.
Given a 2D integer array grid of size n x n, return true if it is an X-Matrix, otherwise return false.
Example 1:
Input: grid = [[2,0,0,1],[0,3,1,0],[0,5,2,0],[4,0,0,2]]
Output: true
Explanation: Refer to the diagram above.
An X-Matrix should have the green elements (diagonals) be non-zero and the red elements be 0.
Thus, grid is an X-Matrix.
Example 2:
Input: grid = [[5,7,0],[0,3,1],[0,5,0]]
Output: false
Explanation: Refer to the diagram above.
An X-Matrix should have the green elements (diagonals) be non-zero and the red elements be 0.
Thus, grid is not an X-Matrix.
Code
1
2
3