2319. Check if Matrix Is X-Matrix
Question
A square matrix is said to be an X-Matrix if both of the following conditions hold:
- All the elements in the diagonals of the matrix are non-zero.
- All other elements are 0.
Given a 2D integer array
grid
of sizen x n
representing a square matrix, returntrue
* ifgrid
is an X-Matrix*. Otherwise, returnfalse
.
Solution
遍历,直接判断是否在对角线上,如果在且位置为0,则返回false。
如果不在对角线上,且位置部位0,则返回false。
Code
1 | class Solution { |
2319. Check if Matrix Is X-Matrix
https://xuanhe95.github.io/2022/06/26/2319-Check-if-Matrix-Is-X-Matrix/