- 原题题目
- 代码实现(首刷自解 做了很久)
原题题目
代码实现(首刷自解 做了很久)
class Solution {
public:
vector findDiagonalOrder(vector>& mat) {
vector ret;
int level = 0, maxlevel = mat.size() + mat[0].size() - 2;
bool dirup = true;
int x = 0, y = 0;
while (level <= maxlevel) {
if (dirup) {
x = min(static_cast(mat.size() - 1), level);
y = level - x;
} else {
y = min(static_cast(mat[0].size() - 1), level);
x = level - y;
}
while (x >= 0 && x < mat.size() && y >= 0 && y < mat[0].size()) {
ret.emplace_back(mat[x][y]);
if (dirup) {
--x; ++y;
} else {
++x; --y;
}
}
++level;
dirup ^= true;
}
return ret;
}
};



