-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path498+Diagonal Traverse.cpp
More file actions
49 lines (46 loc) · 1.48 KB
/
498+Diagonal Traverse.cpp
File metadata and controls
49 lines (46 loc) · 1.48 KB
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
class Solution {
private:
int mM, mN;
public:
inline bool isValid(int i, int j) {
if (i >= 0 && i < mM && j >= 0 && j < mN) return true;
return false;
}
vector<int> findDiagonalOrder(vector<vector<int>>& mat) {
vector<int> res;
mM = mat.size();
mN = mat[0].size();
int sum = mM * mN;
int cnt = 0;
int lineNo = 1;
while(cnt != sum) {
if (lineNo & 1) { // 奇数
int startI = lineNo - 1, startJ = 0;
while (!isValid(startI, startJ)) { //跳过非法点
startI--;
startJ++;
}
while (isValid(startI, startJ)) { //进入合法点
res.emplace_back(mat[startI][startJ]);
cnt++;
startI--;
startJ++;
}
} else { // 偶数
int startI = 0, startJ = lineNo - 1;
while (!isValid(startI, startJ)) { //跳过非法点
startI++;
startJ--;
}
while (isValid(startI, startJ)) { //进入合法点
res.emplace_back(mat[startI][startJ]);
cnt++;
startI++;
startJ--;
}
}
lineNo++;
}
return res;
}
};