classSolution: deffindDiagonalOrder(self, mat: List[List[int]]) -> List[int]: ans = [] m, n = len(mat), len(mat[0]) for i inrange(m + n - 1): if i % 2: x = 0if i < n else i - n + 1 y = i if i < n else n - 1 while x < m and y >= 0: ans.append(mat[x][y]) x += 1 y -= 1 else: x = i if i < m else m - 1 y = 0if i < m else i - m + 1 while x >= 0and y < n: ans.append(mat[x][y]) x -= 1 y += 1 return ans
classSolution { publicint[] findDiagonalOrder(int[][] mat) { intm= mat.length; intn= mat[0].length; int[] res = newint[m * n]; intpos=0; for (inti=0; i < m + n - 1; i++) { if (i % 2 == 1) { intx= i < n ? 0 : i - n + 1; inty= i < n ? i : n - 1; while (x < m && y >= 0) { res[pos] = mat[x][y]; pos++; x++; y--; } } else { intx= i < m ? i : m - 1; inty= i < m ? 0 : i - m + 1; while (x >= 0 && y < n) { res[pos] = mat[x][y]; pos++; x--; y++; } } } return res; } }
classSolution { public: vector<int> findDiagonalOrder(vector<vector<int>>& mat){ int m = mat.size(); int n = mat[0].size(); vector<int> res; for (int i = 0; i < m + n - 1; i++) { if (i % 2) { int x = i < n ? 0 : i - n + 1; int y = i < n ? i : n - 1; while (x < m && y >= 0) { res.emplace_back(mat[x][y]); x++; y--; } } else { int x = i < m ? i : m - 1; int y = i < m ? 0 : i - m + 1; while (x >= 0 && y < n) { res.emplace_back(mat[x][y]); x--; y++; } } } return res; } };
publicclassSolution { publicint[] FindDiagonalOrder(int[][] mat) { int m = mat.Length; int n = mat[0].Length; int[] res = newint[m * n]; int pos = 0; for (int i = 0; i < m + n - 1; i++) { if (i % 2 == 1) { int x = i < n ? 0 : i - n + 1; int y = i < n ? i : n - 1; while (x < m && y >= 0) { res[pos] = mat[x][y]; pos++; x++; y--; } } else { int x = i < m ? i : m - 1; int y = i < m ? 0 : i - m + 1; while (x >= 0 && y < n) { res[pos] = mat[x][y]; pos++; x--; y++; } } } return res; } }
int* findDiagonalOrder(int** mat, int matSize, int* matnSize, int* returnSize) { int m = matSize; int n = matnSize[0]; int *res = (int *)malloc(sizeof(int) * m * n); int pos = 0; for (int i = 0; i < m + n - 1; i++) { if (i % 2) { int x = i < n ? 0 : i - n + 1; int y = i < n ? i : n - 1; while (x < m && y >= 0) { res[pos] = mat[x][y]; pos++; x++; y--; } } else { int x = i < m ? i : m - 1; int y = i < m ? 0 : i - m + 1; while (x >= 0 && y < n) { res[pos] = mat[x][y]; pos++; x--; y++; } } } *returnSize = m * n; return res; }
funcfindDiagonalOrder(mat [][]int) []int { m, n := len(mat), len(mat[0]) ans := make([]int, 0, m*n) for i := 0; i < m+n-1; i++ { if i%2 == 1 { x := max(i-n+1, 0) y := min(i, n-1) for x < m && y >= 0 { ans = append(ans, mat[x][y]) x++ y-- } } else { x := min(i, m-1) y := max(i-m+1, 0) for x >= 0 && y < n { ans = append(ans, mat[x][y]) x-- y++ } } } return ans }
funcmin(a, b int)int { if a > b { return b } return a }
funcmax(a, b int)int { if b > a { return b } return a }
var findDiagonalOrder = function(mat) { const m = mat.length; const n = mat[0].length; const res = newArray(m * n).fill(0); let pos = 0; for (let i = 0; i < m + n - 1; i++) { if (i % 2 === 1) { let x = i < n ? 0 : i - n + 1; let y = i < n ? i : n - 1; while (x < m && y >= 0) { res[pos] = mat[x][y]; pos++; x++; y--; } } else { let x = i < m ? i : m - 1; let y = i < m ? 0 : i - m + 1; while (x >= 0 && y < n) { res[pos] = mat[x][y]; pos++; x--; y++; } } } return res; };
复杂度分析
时间复杂度:O(m \times n),其中 m 为矩阵行的数量,n 为矩阵列的数量。需要遍历一遍矩阵中的所有元素,需要的时间复杂度为 O(m \times n)。