-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDiagonal Traverse.cpp
More file actions
47 lines (41 loc) · 1.18 KB
/
Copy pathDiagonal Traverse.cpp
File metadata and controls
47 lines (41 loc) · 1.18 KB
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
class Solution
{
public:
vector<int> findDiagonalOrder(vector<vector<int>> &matrix)
{
vector<int> results;
if (matrix.size() == 0 || matrix[0].size() == 0)
{
return results;
}
bool flag = true;
int row = 0;
int col = 0;
while (row < matrix.size() && col < matrix[0].size())
{
results.push_back(matrix[row][col]);
int newr = row + (flag == true ? -1 : 1);
int newc = col + (flag == true ? 1 : -1);
if (newr < 0 || newr == matrix.size() || newc < 0 || newc == matrix[0].size())
{
if (flag == true)
{
row += (col == matrix[0].size() - 1 ? 1 : 0);
col += (col < matrix[0].size() - 1 ? 1 : 0);
}
else
{
col += (row == matrix.size() - 1 ? 1 : 0);
row += (row < matrix.size() - 1 ? 1 : 0);
}
flag = !flag;
}
else
{
row = newr;
col = newc;
}
}
return results;
}
};