forked from lzl124631x/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paths1.cpp
22 lines (22 loc) · 746 Bytes
/
s1.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class Solution {
public:
vector<int> findDiagonalOrder(vector<vector<int>>& matrix) {
if (matrix.empty()) return {};
vector<int> ans;
int M = matrix.size(), N = matrix[0].size(), end = M * N;
bool topRight = true;
for (int i = 0, j = 0, c = 0; c < end; ++c) {
ans.push_back(matrix[i][j]);
if (topRight) {
if (j == N - 1) ++i, topRight = !topRight;
else if (i == 0) ++j, topRight = !topRight;
else --i, ++j;
} else {
if (i == M - 1) ++j, topRight = !topRight;
else if (j == 0) ++i, topRight = !topRight;
else ++i, --j;
}
}
return ans;
}
};