-
Notifications
You must be signed in to change notification settings - Fork 0
/
498.hpp
46 lines (38 loc) · 991 Bytes
/
498.hpp
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
//
// Created by bai on 17-6-27.
//
#ifndef LEETCODE_498_HPP
#define LEETCODE_498_HPP
#include <iostream>
#include <queue>
#include <algorithm>
#include <vector>
#include <unordered_map>
using namespace std;
class Solution {
public:
vector<int> findDiagonalOrder(vector<vector<int>> &matrix) {
auto m = matrix.size();
if (m == 0) {
return vector<int>();
}
auto n = matrix[0].size();
vector<int> res(m * n);
int id = 0;
for (int i = 0; i < m + n - 1; ++i) {
int lb = max(0, (int) (i - n + 1));
int ub = min(i, (int) m - 1);
if (i % 2 == 0) {
for (int j = ub; j >= lb; --j) {
res[id++] = matrix[j][i - j];
}
} else {
for (int j = lb; j <= ub; ++j) {
res[id++] = matrix[j][i - j];
}
}
}
return res;
}
};
#endif //LEETCODE_498_HPP