Skip to content

Commit bcdb93d

Browse files
committed
add comment blocks for better explanation
Signed-off-by: rajput-hemant <rajput.hemant2001@gmail.com>
1 parent 9cf8748 commit bcdb93d

File tree

1 file changed

+9
-8
lines changed

1 file changed

+9
-8
lines changed

src/0401-0500/498 - Diagonal Traverse/diagonal_traverse.rs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,41 +11,42 @@ impl Solution {
1111
let (row_len, col_len) = (mat.len(), mat[0].len());
1212

1313
while row < row_len && col < col_len {
14+
// push the current element to the result vector
1415
result.push(mat[row][col]);
1516

1617
match direction {
1718
Direction::Up => {
18-
// if we are at the top right corner, we need to go down
19-
// else we need to go right
2019
if row == 0 || col == col_len - 1 {
20+
// if we are at the top row or the rightmost column, change direction to "Down"
2121
direction = Direction::Down;
2222

23-
// if we are at the top right corner, we need to go down
24-
// else we need to go right
2523
if col == col_len - 1 {
24+
// if at the rightmost column, move to the next row
2625
row += 1;
2726
} else {
27+
// otherwise, move to the next column
2828
col += 1;
2929
}
3030
} else {
31+
// move diagonally upward
3132
row -= 1;
3233
col += 1;
3334
}
3435
}
3536
Direction::Down => {
36-
// if we are at the bottom left corner, we need to go up
37-
// else we need to go down
3837
if col == 0 || row == row_len - 1 {
38+
// if we are at the leftmost column or the bottom row, change direction to "Up"
3939
direction = Direction::Up;
4040

41-
// if we are at the bottom left corner, we need to go right
42-
// else we need to go down
4341
if row == row_len - 1 {
42+
// if at the bottom row, move to the next column
4443
col += 1;
4544
} else {
45+
// otherwise, move to the next row
4646
row += 1;
4747
}
4848
} else {
49+
// move diagonally downward
4950
row += 1;
5051
col -= 1;
5152
}

0 commit comments

Comments
 (0)