当前位置:网站首页>Force buckle 2319 Judge whether the matrix is an X matrix

Force buckle 2319 Judge whether the matrix is an X matrix

2022-07-07 20:06:00 Tomorrowave

2319. Judge whether the matrix is a X matrix

If a square matrix satisfies the following All Conditions , It is called a X matrix :

 All the elements on the diagonal of the matrix are   No  0
 All other elements in the matrix are  0

Give you a size of n x n A two-dimensional array of integers grid , Represents a square matrix . If grid It's a X matrix , return true ; otherwise , return false .

 Insert picture description here

Knowledge points involved :

Two dimensional matrix for loop

Code section

class Solution:
    def checkXMatrix(self, grid: List[List[int]]) -> bool:
        for i in range(len(grid[0])):
            for j in range(len(grid)):
                if i==j or len(grid)-j-1==i :
                    if grid[i][j]==0:
                        return False
                else:
                    if (grid[i][j]!=0):
                        return False
        return True
原网站

版权声明
本文为[Tomorrowave]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/188/202207071753356693.html

随机推荐