1572. Matrix Diagonal Sum Easy

@problem@discussion
#Array#Matrix



1/**
2 * [1572] Matrix Diagonal Sum
3 *
4 * Given a square matrix mat, return the sum of the matrix diagonals.
5 * 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.
6 *  
7 * Example 1:
8 * <img alt="" src="https://assets.leetcode.com/uploads/2020/08/14/sample_1911.png" style="width: 336px; height: 174px;" />
9 * Input: mat = [[1,2,3],
10 *               [4,5,6],
11 *               [7,8,9]]
12 * Output: 25
13 * Explanation: Diagonals sum: 1 + 5 + 9 + 3 + 7 = 25
14 * Notice that element mat[1][1] = 5 is counted only once.
15 * 
16 * Example 2:
17 * 
18 * Input: mat = [[1,1,1,1],
19 *               [1,1,1,1],
20 *               [1,1,1,1],
21 *               [1,1,1,1]]
22 * Output: 8
23 * 
24 * Example 3:
25 * 
26 * Input: mat = [[5]]
27 * Output: 5
28 * 
29 *  
30 * Constraints:
31 * 
32 * 	n == mat.length == mat[i].length
33 * 	1 <= n <= 100
34 * 	1 <= mat[i][j] <= 100
35 * 
36 */
37pub struct Solution {}
38
39// problem: https://leetcode.com/problems/matrix-diagonal-sum/
40// discuss: https://leetcode.com/problems/matrix-diagonal-sum/discuss/?currentPage=1&orderBy=most_votes&query=
41
42// submission codes start here
43
44impl Solution {
45    pub fn diagonal_sum(mat: Vec<Vec<i32>>) -> i32 {
46        0
47    }
48}
49
50// submission codes end
51
52#[cfg(test)]
53mod tests {
54    use super::*;
55
56    #[test]
57    fn test_1572() {
58    }
59}
60


Back
© 2025 bowen.ge All Rights Reserved.