File tree Expand file tree Collapse file tree 1 file changed +38
-0
lines changed Expand file tree Collapse file tree 1 file changed +38
-0
lines changed Original file line number Diff line number Diff line change
1
+ /**
2
+ * @param {number[][] } matrix
3
+ * @return {number[] }
4
+ */
5
+ var findDiagonalOrder = function ( matrix ) {
6
+ let row = 0 ;
7
+ let col = 0 ;
8
+ let up = true ;
9
+ let arr = [ ]
10
+
11
+ if ( matrix . length <= 1 || matrix [ 0 ] . length <= 1 ) {
12
+ return matrix . flat ( )
13
+ }
14
+
15
+ while ( true ) {
16
+ arr . push ( matrix [ row ] [ col ] ) ;
17
+ if ( ( row === 0 || row === matrix . length - 1 ) && col !== matrix [ 0 ] . length - 1 ) {
18
+
19
+ col ++ ;
20
+ up = ! up ;
21
+ arr . push ( matrix [ row ] [ col ] ) ;
22
+ } else if ( col === 0 || col === matrix [ 0 ] . length - 1 ) {
23
+ row ++ ;
24
+ up = ! up ;
25
+ arr . push ( matrix [ row ] [ col ] ) ;
26
+ }
27
+
28
+ if ( row === matrix . length - 1 && col === matrix [ 0 ] . length - 1 ) {
29
+ break ;
30
+ }
31
+
32
+ row = up ? row - 1 : row + 1 ;
33
+ col = up ? col + 1 : col - 1 ;
34
+ }
35
+
36
+ return arr
37
+
38
+ } ;
You can’t perform that action at this time.
0 commit comments