-
Notifications
You must be signed in to change notification settings - Fork 0
/
498.cpp
66 lines (60 loc) · 1.13 KB
/
498.cpp
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
63
64
65
66
#include <algorithm>
#include <iostream>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
vector<int> findDiagonalOrder(const vector<vector<int>>& matrix) {
vector<int> results;
int m = matrix.size();
if (m == 0) {
return results;
}
int n = matrix[0].size();
int i = 0, j = 0;
bool up = true;
int total = m*n;
while (true) {
results.push_back(matrix[i][j]);
//cout << i << ", " << j << endl;
if (results.size() == total ) {
break;
}
if (up) {
--i;
++j;
} else {
++i;
--j;
}
if (i < 0 || i == m || j < 0 || j == n) {
up = !up;
}
if (i < 0 && j == n) {
i = 1;
j = n - 1;
} else if (i == m && j < 0) {
i = m - 1;
j = 1;
} else {
i = max(i, 0);
if (i == m) {
i = m - 1;
j = j + 2;
}
j = max(j, 0);
if (j == n) {
j = n - 1;
i = i + 2;
}
}
}
return results;
}
int main(int argc, char const* argv[]) {
for (auto i : findDiagonalOrder({{1}, {2}})) {
cout << i << endl;
}
return 0;
}