-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheck_if_matrix_is_x_matrix.py
52 lines (41 loc) · 1.68 KB
/
check_if_matrix_is_x_matrix.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
'''
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 size n x n representing a square matrix, return true if grid is an X-Matrix. Otherwise, return false.
'''
class Solution:
def isD(self,i,j,l):
return i == j or i+j == l-1
def checkXMatrix(self, grid: List[List[int]]) -> bool:
l = len(grid)
for i in range(l):
for j in range(l):
if self.isD(i,j,l):
if grid[i][j] == 0:
return False
else:
if grid[i][j]!= 0:
return False
return True
------------------------------------
class Solution:
def checkXMatrix(self, grid: List[List[int]]) -> bool:
n = len(grid)
for i in range(n):
for j in range(n):
if i == j or i + j == n-1:
if grid[i][j] == 0:
return False
else:
if grid[i][j] != 0:
return False
return True
---------------------------------------------
def checkXMatrix(self, grid: List[List[int]]) -> bool:
m, n = len(grid), len(grid[0]) # problem states that m == n
def isDiagonal(i,j):
return i == j or (i + j + 1 == m)
def isValid(i,j):
return isDiagonal(i,j) == bool(grid[i][j]) # if it's diagonal, it's != 0; if it's not diagonal, it's 0
return all(isValid(i,j) for i,j in product(range(m), range(n)))