@@ -11,41 +11,42 @@ impl Solution {
11
11
let ( row_len, col_len) = ( mat. len ( ) , mat[ 0 ] . len ( ) ) ;
12
12
13
13
while row < row_len && col < col_len {
14
+ // push the current element to the result vector
14
15
result. push ( mat[ row] [ col] ) ;
15
16
16
17
match direction {
17
18
Direction :: Up => {
18
- // if we are at the top right corner, we need to go down
19
- // else we need to go right
20
19
if row == 0 || col == col_len - 1 {
20
+ // if we are at the top row or the rightmost column, change direction to "Down"
21
21
direction = Direction :: Down ;
22
22
23
- // if we are at the top right corner, we need to go down
24
- // else we need to go right
25
23
if col == col_len - 1 {
24
+ // if at the rightmost column, move to the next row
26
25
row += 1 ;
27
26
} else {
27
+ // otherwise, move to the next column
28
28
col += 1 ;
29
29
}
30
30
} else {
31
+ // move diagonally upward
31
32
row -= 1 ;
32
33
col += 1 ;
33
34
}
34
35
}
35
36
Direction :: Down => {
36
- // if we are at the bottom left corner, we need to go up
37
- // else we need to go down
38
37
if col == 0 || row == row_len - 1 {
38
+ // if we are at the leftmost column or the bottom row, change direction to "Up"
39
39
direction = Direction :: Up ;
40
40
41
- // if we are at the bottom left corner, we need to go right
42
- // else we need to go down
43
41
if row == row_len - 1 {
42
+ // if at the bottom row, move to the next column
44
43
col += 1 ;
45
44
} else {
45
+ // otherwise, move to the next row
46
46
row += 1 ;
47
47
}
48
48
} else {
49
+ // move diagonally downward
49
50
row += 1 ;
50
51
col -= 1 ;
51
52
}
0 commit comments