-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0498.cpp
More file actions
48 lines (41 loc) · 769 Bytes
/
0498.cpp
File metadata and controls
48 lines (41 loc) · 769 Bytes
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
class Solution {
public:
vector<int> findDiagonalOrder(vector<vector<int>> &mat) {
int m = mat.size();
int n = mat[0].size();
int tot = m * n;
vector<int> ans(tot);
int i = 0;
int j = 0;
int k = 0;
bool up = true;
while (k < tot) {
ans[k] = mat[i][j];
if (up) {
if (!i && j < n - 1) {
j++;
up = false;
} else if (j == n - 1) {
i++;
up = false;
} else {
i--;
j++;
}
} else {
if (i < m - 1 && !j) {
i++;
up = true;
} else if (i == m - 1) {
j++;
up = true;
} else {
i++;
j--;
}
}
k++;
}
return ans;
}
};