File tree 3 files changed +46
-0
lines changed
solution/1500-1599/1572.Matrix Diagonal Sum
3 files changed +46
-0
lines changed Original file line number Diff line number Diff line change @@ -90,6 +90,23 @@ class Solution {
90
90
}
91
91
```
92
92
93
+ ### ** TypeScript**
94
+
95
+ ``` ts
96
+ function diagonalSum(mat : number [][]): number {
97
+ let n = mat .length ;
98
+ let ans = 0 ;
99
+ for (let i = 0 ; i < n ; i ++ ) {
100
+ ans += mat [i ][i ];
101
+ let j = n - 1 - i ;
102
+ if (i != j ) {
103
+ ans += mat [i ][j ];
104
+ }
105
+ }
106
+ return ans ;
107
+ };
108
+ ```
109
+
93
110
### ** C++**
94
111
95
112
``` cpp
Original file line number Diff line number Diff line change @@ -78,6 +78,23 @@ class Solution {
78
78
}
79
79
```
80
80
81
+ ### ** TypeScript**
82
+
83
+ ``` ts
84
+ function diagonalSum(mat : number [][]): number {
85
+ let n = mat .length ;
86
+ let ans = 0 ;
87
+ for (let i = 0 ; i < n ; i ++ ) {
88
+ ans += mat [i ][i ];
89
+ let j = n - 1 - i ;
90
+ if (i != j ) {
91
+ ans += mat [i ][j ];
92
+ }
93
+ }
94
+ return ans ;
95
+ };
96
+ ```
97
+
81
98
### ** C++**
82
99
83
100
``` cpp
Original file line number Diff line number Diff line change
1
+ function diagonalSum ( mat : number [ ] [ ] ) : number {
2
+ let n = mat . length ;
3
+ let ans = 0 ;
4
+ for ( let i = 0 ; i < n ; i ++ ) {
5
+ ans += mat [ i ] [ i ] ;
6
+ let j = n - 1 - i ;
7
+ if ( i != j ) {
8
+ ans += mat [ i ] [ j ] ;
9
+ }
10
+ }
11
+ return ans ;
12
+ } ;
You can’t perform that action at this time.
0 commit comments