Skip to content

Commit cd5572f

Browse files
authored
Merge pull request #135 from gostool/leetcode1572
add: leetcode 1572 solution
2 parents 776fe07 + 36f3a4c commit cd5572f

File tree

3 files changed

+141
-0
lines changed

3 files changed

+141
-0
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package leetcode
2+
3+
func diagonalSum(mat [][]int) int {
4+
n := len(mat)
5+
ans := 0
6+
for pi := 0; pi < n; pi++ {
7+
ans += mat[pi][pi]
8+
}
9+
for si, sj := n-1, 0; sj < n; si, sj = si-1, sj+1 {
10+
ans += mat[si][sj]
11+
}
12+
if n%2 == 0 {
13+
return ans
14+
}
15+
return ans - mat[n/2][n/2]
16+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package leetcode
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
)
7+
8+
type question1572 struct {
9+
para1572
10+
ans1572
11+
}
12+
13+
// para 是参数
14+
type para1572 struct {
15+
mat [][]int
16+
}
17+
18+
// ans 是答案
19+
// one 代表第一个答案
20+
type ans1572 struct {
21+
one int
22+
}
23+
24+
func Test_Problem1572(t *testing.T) {
25+
26+
qs := []question1572{
27+
28+
{
29+
para1572{[][]int{{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}},
30+
ans1572{25},
31+
},
32+
33+
{
34+
para1572{[][]int{{1, 1, 1, 1}, {1, 1, 1, 1}, {1, 1, 1, 1}, {1, 1, 1, 1}}},
35+
ans1572{8},
36+
},
37+
38+
{
39+
para1572{[][]int{{5}}},
40+
ans1572{5},
41+
},
42+
}
43+
44+
fmt.Printf("------------------------Leetcode Problem 1572------------------------\n")
45+
46+
for _, q := range qs {
47+
_, p := q.ans1572, q.para1572
48+
fmt.Printf("【input】:%v 【output】:%v \n", p, diagonalSum(p.mat))
49+
}
50+
fmt.Printf("\n\n\n")
51+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
# [1572. Matrix Diagonal Sum](https://leetcode-cn.com/problems/matrix-diagonal-sum/)
2+
3+
4+
## 题目
5+
6+
Given a square matrix mat, return the sum of the matrix diagonals.
7+
8+
Only include the sum of all the elements on the primary diagonal and all the elements on the secondary diagonal that are not part of the primary diagonal.
9+
10+
**Example 1:**
11+
12+
```
13+
Input: mat = [[1,2,3],
14+
[4,5,6],
15+
[7,8,9]]
16+
Output: 25
17+
Explanation: Diagonals sum: 1 + 5 + 9 + 3 + 7 = 25
18+
Notice that element mat[1][1] = 5 is counted only once.
19+
```
20+
21+
**Example 2:**
22+
23+
```
24+
Input: mat = [[1,1,1,1],
25+
[1,1,1,1],
26+
[1,1,1,1],
27+
[1,1,1,1]]
28+
Output: 8
29+
```
30+
31+
**Example 3:**
32+
33+
```
34+
Input: mat = [[5]]
35+
Output: 5
36+
```
37+
38+
**Constraints:**
39+
40+
- n == mat.length == mat[i].length
41+
- 1 <= n <= 100
42+
- 1 <= mat[i][j] <= 100
43+
44+
## 题目大意
45+
46+
给你一个正方形矩阵 mat,请你返回矩阵对角线元素的和。
47+
48+
请你返回在矩阵主对角线上的元素和副对角线上且不在主对角线上元素的和
49+
50+
## 解题思路
51+
52+
- 根据题意,把主对角线和副对角线上的元素相加
53+
- 如果正方形矩阵的长度n为奇数,相加的结果需要减去mat[n/2][n/2]
54+
55+
## 代码
56+
57+
```go
58+
package leetcode
59+
60+
func diagonalSum(mat [][]int) int {
61+
n := len(mat)
62+
ans := 0
63+
for pi := 0; pi < n; pi++ {
64+
ans += mat[pi][pi]
65+
}
66+
for si, sj := n - 1, 0; sj < n; si, sj = si - 1, sj + 1 {
67+
ans += mat[si][sj]
68+
}
69+
if n % 2 == 0 {
70+
return ans
71+
}
72+
return ans - mat[n / 2][n / 2]
73+
}
74+
```

0 commit comments

Comments
 (0)