lurenaa的博客

实现

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
class Solution {
public:
vector<int> findDiagonalOrder(vector<vector<int>>& matrix) {
vector<int> v;
if(!matrix.size()) return v;
bool r = false;
for(int i = 2; i <= matrix.size() + matrix[0].size(); ++i)
{
int m = r ? 1 : i - 1;
while(1) {
int n = i - m;
if(m - 1 < matrix.size() && n - 1 < matrix[0].size())
v.push_back(matrix[m - 1][n - 1]);
if(r) {
++m;
if(m == i)
break;
} else {
--m;
if(m == 0)
break;
}
}
r = !r;
}
return v;
}
};

Accepted

32/32 cases passed (532 ms)

Your runtime beats 5.13 % of cpp submissions

Your memory usage beats 8.42 % of cpp submissions (16 MB)