- 原题题目
- 代码实现(首刷自解 做了很久)
原题题目
代码实现(首刷自解 做了很久)
class Solution {
public:
vector<int> findDiagonalOrder(vector<vector<int>>& mat) {
vector<int> 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<int>(mat.size() - 1), level);
y = level - x;
} else {
y = min(static_cast<int>(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;
}
};
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)