Skip to content

Commit 4330393

Browse files
committed
Update solution 1572
1 parent cd5572f commit 4330393

File tree

12 files changed

+157
-69
lines changed

12 files changed

+157
-69
lines changed

README.md

Lines changed: 38 additions & 38 deletions
Large diffs are not rendered by default.
Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,59 @@
1-
# [1572. Matrix Diagonal Sum](https://leetcode-cn.com/problems/matrix-diagonal-sum/)
1+
# [1572. Matrix Diagonal Sum](https://leetcode.com/problems/matrix-diagonal-sum/)
22

33

44
## 题目
55

6-
Given a square matrix mat, return the sum of the matrix diagonals.
6+
Given a square matrix `mat`, return the sum of the matrix diagonals.
77

88
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.
99

1010
**Example 1:**
1111

12+
![https://assets.leetcode.com/uploads/2020/08/14/sample_1911.png](https://assets.leetcode.com/uploads/2020/08/14/sample_1911.png)
13+
1214
```
1315
Input: mat = [[1,2,3],
14-
[4,5,6],
15-
[7,8,9]]
16+
  [4,5,6],
17+
  [7,8,9]]
1618
Output: 25
17-
Explanation: Diagonals sum: 1 + 5 + 9 + 3 + 7 = 25
19+
Explanation:Diagonals sum: 1 + 5 + 9 + 3 + 7 = 25
1820
Notice that element mat[1][1] = 5 is counted only once.
21+
1922
```
2023

2124
**Example 2:**
2225

2326
```
2427
Input: mat = [[1,1,1,1],
25-
[1,1,1,1],
26-
[1,1,1,1],
27-
[1,1,1,1]]
28+
  [1,1,1,1],
29+
  [1,1,1,1],
30+
  [1,1,1,1]]
2831
Output: 8
32+
2933
```
3034

3135
**Example 3:**
3236

3337
```
3438
Input: mat = [[5]]
3539
Output: 5
40+
3641
```
3742

3843
**Constraints:**
3944

40-
- n == mat.length == mat[i].length
41-
- 1 <= n <= 100
42-
- 1 <= mat[i][j] <= 100
45+
- `n == mat.length == mat[i].length`
46+
- `1 <= n <= 100`
47+
- `1 <= mat[i][j] <= 100`
4348

4449
## 题目大意
4550

46-
给你一个正方形矩阵 mat,请你返回矩阵对角线元素的和。
47-
48-
请你返回在矩阵主对角线上的元素和副对角线上且不在主对角线上元素的和
51+
给你一个正方形矩阵 mat,请你返回矩阵对角线元素的和。请你返回在矩阵主对角线上的元素和副对角线上且不在主对角线上元素的和。
4952

5053
## 解题思路
5154

52-
- 根据题意,把主对角线和副对角线上的元素相加
53-
- 如果正方形矩阵的长度n为奇数,相加的结果需要减去mat[n/2][n/2]
55+
- 简单题。根据题意把主对角线和副对角线上的元素相加
56+
- 如果正方形矩阵的长度 n 为奇数,相加的结果需要减去 mat[n/2][n/2]
5457

5558
## 代码
5659

@@ -63,12 +66,12 @@ func diagonalSum(mat [][]int) int {
6366
for pi := 0; pi < n; pi++ {
6467
ans += mat[pi][pi]
6568
}
66-
for si, sj := n - 1, 0; sj < n; si, sj = si - 1, sj + 1 {
69+
for si, sj := n-1, 0; sj < n; si, sj = si-1, sj+1 {
6770
ans += mat[si][sj]
6871
}
69-
if n % 2 == 0 {
72+
if n%2 == 0 {
7073
return ans
7174
}
72-
return ans - mat[n / 2][n / 2]
75+
return ans - mat[n/2][n/2]
7376
}
7477
```

website/content/ChapterFour/1500~1599/1551.Minimum-Operations-to-Make-Array-Equal.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,5 +65,5 @@ func minOperations(n int) int {
6565
----------------------------------------------
6666
<div style="display: flex;justify-content: space-between;align-items: center;">
6767
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/1500~1599/1539.Kth-Missing-Positive-Number/">⬅️上一页</a></p>
68-
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/1500~1599/1573.Number-of-Ways-to-Split-a-String/">下一页➡️</a></p>
68+
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/1500~1599/1572.Matrix-Diagonal-Sum/">下一页➡️</a></p>
6969
</div>
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
# [1572. Matrix Diagonal Sum](https://leetcode.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+
![https://assets.leetcode.com/uploads/2020/08/14/sample_1911.png](https://assets.leetcode.com/uploads/2020/08/14/sample_1911.png)
13+
14+
```
15+
Input: mat = [[1,2,3],
16+
  [4,5,6],
17+
  [7,8,9]]
18+
Output: 25
19+
Explanation:Diagonals sum: 1 + 5 + 9 + 3 + 7 = 25
20+
Notice that element mat[1][1] = 5 is counted only once.
21+
22+
```
23+
24+
**Example 2:**
25+
26+
```
27+
Input: mat = [[1,1,1,1],
28+
  [1,1,1,1],
29+
  [1,1,1,1],
30+
  [1,1,1,1]]
31+
Output: 8
32+
33+
```
34+
35+
**Example 3:**
36+
37+
```
38+
Input: mat = [[5]]
39+
Output: 5
40+
41+
```
42+
43+
**Constraints:**
44+
45+
- `n == mat.length == mat[i].length`
46+
- `1 <= n <= 100`
47+
- `1 <= mat[i][j] <= 100`
48+
49+
## 题目大意
50+
51+
给你一个正方形矩阵 mat,请你返回矩阵对角线元素的和。请你返回在矩阵主对角线上的元素和副对角线上且不在主对角线上元素的和。
52+
53+
## 解题思路
54+
55+
- 简单题。根据题意,把主对角线和副对角线上的元素相加。
56+
- 如果正方形矩阵的长度 n 为奇数,相加的结果需要减去 mat[n/2][n/2]
57+
58+
## 代码
59+
60+
```go
61+
package leetcode
62+
63+
func diagonalSum(mat [][]int) int {
64+
n := len(mat)
65+
ans := 0
66+
for pi := 0; pi < n; pi++ {
67+
ans += mat[pi][pi]
68+
}
69+
for si, sj := n-1, 0; sj < n; si, sj = si-1, sj+1 {
70+
ans += mat[si][sj]
71+
}
72+
if n%2 == 0 {
73+
return ans
74+
}
75+
return ans - mat[n/2][n/2]
76+
}
77+
```
78+
79+
80+
----------------------------------------------
81+
<div style="display: flex;justify-content: space-between;align-items: center;">
82+
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/1500~1599/1551.Minimum-Operations-to-Make-Array-Equal/">⬅️上一页</a></p>
83+
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/1500~1599/1573.Number-of-Ways-to-Split-a-String/">下一页➡️</a></p>
84+
</div>

website/content/ChapterFour/1500~1599/1573.Number-of-Ways-to-Split-a-String.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,6 @@ func numWays(s string) int {
110110

111111
----------------------------------------------
112112
<div style="display: flex;justify-content: space-between;align-items: center;">
113-
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/1500~1599/1551.Minimum-Operations-to-Make-Array-Equal/">⬅️上一页</a></p>
113+
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/1500~1599/1572.Matrix-Diagonal-Sum/">⬅️上一页</a></p>
114114
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/1500~1599/1579.Remove-Max-Number-of-Edges-to-Keep-Graph-Fully-Traversable/">下一页➡️</a></p>
115115
</div>

website/content/ChapterTwo/Array.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,7 @@ weight: 1
162162
|1486|XOR Operation in an Array|[Go]({{< relref "/ChapterFour/1400~1499/1486.XOR-Operation-in-an-Array.md" >}})|Easy||||83.9%|
163163
|1512|Number of Good Pairs|[Go]({{< relref "/ChapterFour/1500~1599/1512.Number-of-Good-Pairs.md" >}})|Easy||||87.6%|
164164
|1539|Kth Missing Positive Number|[Go]({{< relref "/ChapterFour/1500~1599/1539.Kth-Missing-Positive-Number.md" >}})|Easy||||54.7%|
165+
|1572|Matrix Diagonal Sum|[Go]({{< relref "/ChapterFour/1500~1599/1572.Matrix-Diagonal-Sum.md" >}})|Easy||||77.8%|
165166
|1608|Special Array With X Elements Greater Than or Equal X|[Go]({{< relref "/ChapterFour/1600~1699/1608.Special-Array-With-X-Elements-Greater-Than-or-Equal-X.md" >}})|Easy||||61.3%|
166167
|1619|Mean of Array After Removing Some Elements|[Go]({{< relref "/ChapterFour/1600~1699/1619.Mean-of-Array-After-Removing-Some-Elements.md" >}})|Easy||||64.4%|
167168
|1629|Slowest Key|[Go]({{< relref "/ChapterFour/1600~1699/1629.Slowest-Key.md" >}})|Easy||||58.9%|

website/content/ChapterTwo/Binary_Search.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ func peakIndexInMountainArray(A []int) int {
198198
|1337|The K Weakest Rows in a Matrix|[Go]({{< relref "/ChapterFour/1300~1399/1337.The-K-Weakest-Rows-in-a-Matrix.md" >}})|Easy||||72.0%|
199199
|1482|Minimum Number of Days to Make m Bouquets|[Go]({{< relref "/ChapterFour/1400~1499/1482.Minimum-Number-of-Days-to-Make-m-Bouquets.md" >}})|Medium||||51.5%|
200200
|1631|Path With Minimum Effort|[Go]({{< relref "/ChapterFour/1600~1699/1631.Path-With-Minimum-Effort.md" >}})|Medium||||50.0%|
201-
|1642|Furthest Building You Can Reach|[Go]({{< relref "/ChapterFour/1600~1699/1642.Furthest-Building-You-Can-Reach.md" >}})|Medium||||46.8%|
201+
|1642|Furthest Building You Can Reach|[Go]({{< relref "/ChapterFour/1600~1699/1642.Furthest-Building-You-Can-Reach.md" >}})|Medium||||46.7%|
202202
|1649|Create Sorted Array through Instructions|[Go]({{< relref "/ChapterFour/1600~1699/1649.Create-Sorted-Array-through-Instructions.md" >}})|Hard||||36.8%|
203203
|1658|Minimum Operations to Reduce X to Zero|[Go]({{< relref "/ChapterFour/1600~1699/1658.Minimum-Operations-to-Reduce-X-to-Zero.md" >}})|Medium||||33.4%|
204204
|------------|-------------------------------------------------------|-------| ----------------| ---------------|-------------|-------------|-------------|

website/content/ChapterTwo/Depth_First_Search.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ weight: 9
1111
|:--------:|:------- | :--------: | :----------: | :----: | :-----: | :-----: |:-----: |
1212
|0017|Letter Combinations of a Phone Number|[Go]({{< relref "/ChapterFour/0001~0099/0017.Letter-Combinations-of-a-Phone-Number.md" >}})|Medium||||50.3%|
1313
|0098|Validate Binary Search Tree|[Go]({{< relref "/ChapterFour/0001~0099/0098.Validate-Binary-Search-Tree.md" >}})|Medium| O(n)| O(1)||29.1%|
14-
|0099|Recover Binary Search Tree|[Go]({{< relref "/ChapterFour/0001~0099/0099.Recover-Binary-Search-Tree.md" >}})|Hard| O(n)| O(1)||43.1%|
14+
|0099|Recover Binary Search Tree|[Go]({{< relref "/ChapterFour/0001~0099/0099.Recover-Binary-Search-Tree.md" >}})|Medium| O(n)| O(1)||43.1%|
1515
|0100|Same Tree|[Go]({{< relref "/ChapterFour/0100~0199/0100.Same-Tree.md" >}})|Easy| O(n)| O(1)||54.4%|
1616
|0101|Symmetric Tree|[Go]({{< relref "/ChapterFour/0100~0199/0101.Symmetric-Tree.md" >}})|Easy| O(n)| O(1)||48.8%|
1717
|0104|Maximum Depth of Binary Tree|[Go]({{< relref "/ChapterFour/0100~0199/0104.Maximum-Depth-of-Binary-Tree.md" >}})|Easy| O(n)| O(1)||68.7%|
@@ -63,7 +63,7 @@ weight: 9
6363
|0834|Sum of Distances in Tree|[Go]({{< relref "/ChapterFour/0800~0899/0834.Sum-of-Distances-in-Tree.md" >}})|Hard||||47.0%|
6464
|0839|Similar String Groups|[Go]({{< relref "/ChapterFour/0800~0899/0839.Similar-String-Groups.md" >}})|Hard||||42.2%|
6565
|0841|Keys and Rooms|[Go]({{< relref "/ChapterFour/0800~0899/0841.Keys-and-Rooms.md" >}})|Medium||||66.7%|
66-
|0851|Loud and Rich|[Go]({{< relref "/ChapterFour/0800~0899/0851.Loud-and-Rich.md" >}})|Medium||||53.1%|
66+
|0851|Loud and Rich|[Go]({{< relref "/ChapterFour/0800~0899/0851.Loud-and-Rich.md" >}})|Medium||||53.0%|
6767
|0863|All Nodes Distance K in Binary Tree|[Go]({{< relref "/ChapterFour/0800~0899/0863.All-Nodes-Distance-K-in-Binary-Tree.md" >}})|Medium||||58.5%|
6868
|0872|Leaf-Similar Trees|[Go]({{< relref "/ChapterFour/0800~0899/0872.Leaf-Similar-Trees.md" >}})|Easy||||64.5%|
6969
|0897|Increasing Order Search Tree|[Go]({{< relref "/ChapterFour/0800~0899/0897.Increasing-Order-Search-Tree.md" >}})|Easy||||74.8%|
@@ -74,7 +74,7 @@ weight: 9
7474
|0959|Regions Cut By Slashes|[Go]({{< relref "/ChapterFour/0900~0999/0959.Regions-Cut-By-Slashes.md" >}})|Medium||||67.4%|
7575
|0968|Binary Tree Cameras|[Go]({{< relref "/ChapterFour/0900~0999/0968.Binary-Tree-Cameras.md" >}})|Hard||||40.5%|
7676
|0971|Flip Binary Tree To Match Preorder Traversal|[Go]({{< relref "/ChapterFour/0900~0999/0971.Flip-Binary-Tree-To-Match-Preorder-Traversal.md" >}})|Medium||||50.0%|
77-
|0979|Distribute Coins in Binary Tree|[Go]({{< relref "/ChapterFour/0900~0999/0979.Distribute-Coins-in-Binary-Tree.md" >}})|Medium||||70.1%|
77+
|0979|Distribute Coins in Binary Tree|[Go]({{< relref "/ChapterFour/0900~0999/0979.Distribute-Coins-in-Binary-Tree.md" >}})|Medium||||70.0%|
7878
|0980|Unique Paths III|[Go]({{< relref "/ChapterFour/0900~0999/0980.Unique-Paths-III.md" >}})|Hard| O(n log n)| O(n)||77.1%|
7979
|0987|Vertical Order Traversal of a Binary Tree|[Go]({{< relref "/ChapterFour/0900~0999/0987.Vertical-Order-Traversal-of-a-Binary-Tree.md" >}})|Hard||||39.2%|
8080
|1020|Number of Enclaves|[Go]({{< relref "/ChapterFour/1000~1099/1020.Number-of-Enclaves.md" >}})|Medium||||59.4%|

website/content/ChapterTwo/Math.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ weight: 12
9595
|1641|Count Sorted Vowel Strings|[Go]({{< relref "/ChapterFour/1600~1699/1641.Count-Sorted-Vowel-Strings.md" >}})|Medium||||75.2%|
9696
|1648|Sell Diminishing-Valued Colored Balls|[Go]({{< relref "/ChapterFour/1600~1699/1648.Sell-Diminishing-Valued-Colored-Balls.md" >}})|Medium||||30.8%|
9797
|1680|Concatenation of Consecutive Binary Numbers|[Go]({{< relref "/ChapterFour/1600~1699/1680.Concatenation-of-Consecutive-Binary-Numbers.md" >}})|Medium||||52.3%|
98-
|1685|Sum of Absolute Differences in a Sorted Array|[Go]({{< relref "/ChapterFour/1600~1699/1685.Sum-of-Absolute-Differences-in-a-Sorted-Array.md" >}})|Medium||||63.4%|
98+
|1685|Sum of Absolute Differences in a Sorted Array|[Go]({{< relref "/ChapterFour/1600~1699/1685.Sum-of-Absolute-Differences-in-a-Sorted-Array.md" >}})|Medium||||63.3%|
9999
|1716|Calculate Money in Leetcode Bank|[Go]({{< relref "/ChapterFour/1700~1799/1716.Calculate-Money-in-Leetcode-Bank.md" >}})|Easy||||64.3%|
100100
|------------|-------------------------------------------------------|-------| ----------------| ---------------|-------------|-------------|-------------|
101101

website/content/ChapterTwo/Sort.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ weight: 14
5454
|1647|Minimum Deletions to Make Character Frequencies Unique|[Go]({{< relref "/ChapterFour/1600~1699/1647.Minimum-Deletions-to-Make-Character-Frequencies-Unique.md" >}})|Medium||||55.6%|
5555
|1648|Sell Diminishing-Valued Colored Balls|[Go]({{< relref "/ChapterFour/1600~1699/1648.Sell-Diminishing-Valued-Colored-Balls.md" >}})|Medium||||30.8%|
5656
|1691|Maximum Height by Stacking Cuboids|[Go]({{< relref "/ChapterFour/1600~1699/1691.Maximum-Height-by-Stacking-Cuboids.md" >}})|Hard||||50.6%|
57-
|1710|Maximum Units on a Truck|[Go]({{< relref "/ChapterFour/1700~1799/1710.Maximum-Units-on-a-Truck.md" >}})|Easy||||69.9%|
57+
|1710|Maximum Units on a Truck|[Go]({{< relref "/ChapterFour/1700~1799/1710.Maximum-Units-on-a-Truck.md" >}})|Easy||||70.0%|
5858
|------------|-------------------------------------------------------|-------| ----------------| ---------------|-------------|-------------|-------------|
5959

6060

0 commit comments

Comments
 (0)