Skip to content

Commit 0fb0ce9

Browse files
committed
✨ [1572] Matrix Diagonal Sum
1 parent 6d65475 commit 0fb0ce9

File tree

3 files changed

+58
-2
lines changed

3 files changed

+58
-2
lines changed

1572/my_solution.js

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/**
2+
* @param {number[][]} mat
3+
* @return {number}
4+
*/
5+
var diagonalSum = function (mat) {
6+
let sum = 0, l = 0, r = mat[0].length - 1;
7+
8+
for (let row = 0; row < mat.length; row++) {
9+
// for (let c = 0; c < mat[r].length; c++) {
10+
console.log(`mat[row][l]:${mat[row][l]}, mat[row][r]:${mat[row][r]}`)
11+
if (l === r) { // intersect
12+
sum += mat[row][l];
13+
} else {
14+
sum += mat[row][l] + mat[row][r];
15+
}
16+
17+
l++;
18+
r--;
19+
// }
20+
}
21+
22+
return sum;
23+
};
24+
25+
let x = null;
26+
x = diagonalSum([
27+
[1, 2, 3],
28+
[4, 5, 6],
29+
[7, 8, 9]
30+
])
31+
32+
33+
console.log("Result");
34+
console.log(x);

1572/solution.js

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* @param {number[][]} mat
3+
* @return {number}
4+
*/
5+
var diagonalSum = function (mat) {
6+
let sum = 0, l = 0, r = mat[0].length - 1;
7+
8+
for (let row = 0; row < mat.length; row++) {
9+
if (l === r) {
10+
sum += mat[row][l];
11+
} else {
12+
sum += mat[row][l] + mat[row][r];
13+
}
14+
15+
l++;
16+
r--;
17+
}
18+
19+
return sum;
20+
};
21+

README.md

+3-2
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@
7474
- [1365. How Many Numbers Are Smaller Than the Current Number](./1365/)
7575
- [1512. Number of Good Pairs](./1512/)
7676
- [1569. Number of Ways to Reorder Array to Get Same BST](./1569/)
77+
- [1572. Matrix Diagonal Sum](./1572/)
7778
- [1672. Richest Customer Wealth](./1672/)
7879
- [1791. Find Center of Star Graph](./1791/)
7980
- [1909. Remove One Element to Make the Array Strictly Increasing](./1909/)
@@ -106,7 +107,7 @@ iex ./.../solution.ex
106107

107108
Batch create:
108109
```ssh
109-
chapter=997 && mkdir ./$chapter && touch ./$chapter/my_solution.ex && touch ./$chapter/solution.ex && alias x="iex ./$chapter/my_solution.ex"
110+
chapter=1572 && mkdir ./$chapter && touch ./$chapter/my_solution.ex && touch ./$chapter/solution.ex && alias x="iex ./$chapter/my_solution.ex"
110111
```
111112
> then you can use `x` for quick debug.
112113
@@ -128,7 +129,7 @@ Batch create:
128129
NOTE: JS IS HERE
129130
-->
130131
```ssh
131-
chapter=2448 && mkdir ./$chapter && touch ./$chapter/my_solution.js && touch ./$chapter/solution.js && alias x="node ./$chapter/my_solution.js"
132+
chapter=1572 && mkdir ./$chapter && touch ./$chapter/my_solution.js && touch ./$chapter/solution.js && alias x="node ./$chapter/my_solution.js"
132133
```
133134
> then you can use `x` for quick debug.
134135

0 commit comments

Comments
 (0)