-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2319-CheckIfMatrixIsXMatrix.go
More file actions
65 lines (57 loc) · 2.45 KB
/
2319-CheckIfMatrixIsXMatrix.go
File metadata and controls
65 lines (57 loc) · 2.45 KB
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
53
54
55
56
57
58
59
60
61
62
63
64
65
package main
// 2319. Check if Matrix Is X-Matrix
// 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.
// Example 1:
// <img src="https://assets.leetcode.com/uploads/2022/05/03/ex1.jpg" />
// 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:
// <img src="https://assets.leetcode.com/uploads/2022/05/03/ex2.jpg" />
// 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.
// Constraints:
// n == grid.length == grid[i].length
// 3 <= n <= 100
// 0 <= grid[i][j] <= 10^5
import "fmt"
func checkXMatrix(grid [][]int) bool {
n := len(grid)
for i := 0; i < n; i++ {
for j := 0; j < n; j++ {
// 矩阵对角线上的所有元素都 不是 0 对角线时: true = (v != 0)
// 矩阵中所有其他元素都是 0 非对角线时: false = (v == 0)
if (i == j || i + j == n - 1) == (grid[i][j] == 0) {
return false
}
}
}
return true
}
func main() {
// Example 1:
// <img src="https://assets.leetcode.com/uploads/2022/05/03/ex1.jpg" />
// 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.
fmt.Println(checkXMatrix([][]int{{2,0,0,1},{0,3,1,0},{0,5,2,0},{4,0,0,2}})) // true
// Example 2:
// <img src="https://assets.leetcode.com/uploads/2022/05/03/ex2.jpg" />
// 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.
fmt.Println(checkXMatrix([][]int{{5,7,0},{0,3,1},{0,5,0}})) // false
}