# Code language: Python classSolution: deffindDiagonalOrder(self, mat: List[List[int]]) -> List[int]: dir = ((-1, 1), (1, -1)) n, m = len(mat), len(mat[0]) d, x, y = 0, 0, 0 ans = list() while x < n and y < m: ans.append(mat[x][y]) dx, dy = dir[d] if y + dy == m: x += 1 d = 1 - d elif x + dx == n: y += 1 d = 1 - d elif y + dy == -1: x += 1 d = 1 - d elif x + dx == -1: y += 1 d = 1 - d else: x += dx y += dy return ans
// Code language: C++ classSolution { public: vector<int> findDiagonalOrder(vector<vector<int>>& mat){ vector<vector<int>> dir{{-1, 1}, {1, -1}}; vector<int> ans; for (int d = 0, x = 0, y = 0, n = mat.size(), m = mat[0].size(); x < n && y < m; ) { ans.emplace_back(mat[x][y]); int dx = dir[d][0], dy = dir[d][1]; if (y + dy == m) x++; elseif (x + dx == n) y++; elseif (y + dy == -1) x++; elseif (x + dx == -1) y++; else { x += dx; y += dy; continue; } d = 1 - d; } return ans; } };