题解 | 对角线遍历矩阵
class Solution { public: /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param mat int整型vector<vector<>> * @return int整型vector */ vector<int> diagonalOrder(vector<vector<int> >& mat) { // write code here int row = mat.size(); int column = mat[0].size(); int count = row * column; vector<int> result; bool isUp = true; int x = 0; int y = 0; while (result.size() < count) { result.push_back(mat[x][y]); if (isUp) { if (y == column - 1) { x++; isUp = false; continue; } else if (x == 0) { y++; isUp = false; continue; } x--; y++; } else { if (x == row - 1) { y++; isUp = true; continue; } else if (y == 0) { x++; isUp = true; continue; } y--; x++; } } return result; } };
单纯的循环,注意边界条件