classSolution { public: vector<int> findDiagonalOrder(vector<vector<int>>& mat){ int m = mat.size(), n = mat[0].size(); vector<int> res; bool dir = true; for (int i = 0; i < m + n - 1; i++) { if (dir) { for (int j = max(0, i - m + 1); j <= min(i, n - 1); j++) { int x = i - j, y = j; // i - j < m => j > i - m // if (x >= m || y >= n) { // continue; // } res.push_back(mat[x][y]); } } else { for (int j = min(i, n - 1); j >= max(0, i - m + 1); j--) { int x = i - j, y = j; // if (x >= m || y >= n) { // continue; // } res.push_back(mat[x][y]); } } dir = !dir; } return res; } };