-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution498.cs
62 lines (57 loc) · 1.34 KB
/
Solution498.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
namespace LeetCode.Solutions;
public class Solution498
{
/// <summary>
/// 498. Diagonal Traverse - Medium
/// <a href="https://leetcode.com/problems/diagonal-traverse">See the problem</a>
/// </summary>
public int[] FindDiagonalOrder(int[][] mat)
{
if (mat.Length == 0)
{
return [];
}
int rows = mat.Length;
int cols = mat[0].Length;
int[] result = new int[rows * cols];
int row = 0;
int col = 0;
for (int i = 0; i < result.Length; i++)
{
result[i] = mat[row][col];
if ((row + col) % 2 == 0)
{
if (col == cols - 1)
{
row++;
}
else if (row == 0)
{
col++;
}
else
{
row--;
col++;
}
}
else
{
if (row == rows - 1)
{
col++;
}
else if (col == 0)
{
row++;
}
else
{
row++;
col--;
}
}
}
return result;
}
}