给你一个大小为 m x n 的矩阵 mat ,请以对角线遍历的顺序,用一个数组返回这个矩阵中的所有元素。
示例 1: 输入:mat = [[1,2,3],[4,5,6],[7,8,9]]
输出:[1,2,4,7,5,3,6,8,9]
var findDiagonalOrder = function(mat) {
const m = mat.length - 1, n = mat[0].length - 1, r = new Int32Array((m + 1) * (n + 1))
for (let i = 0, pos = 0; i <= m + n + 1; i++) {
if (i % 2 === 0) {
let x = i < m ? i : m, y = i < m ? 0 : i - m
while (x >= 0 && y <= n) {
r[pos++] = mat[x][y]
x--
y++
}
} else {
let x = i < n ? 0 : i - n, y = i < n ? i : n
while (x <= m && y >= 0) {
r[pos++] = mat[x][y]
x++
y--
}
}
}
return r
};
function findDiagonalOrder(mat: number[][]): number[] {
const m = mat.length - 1, n = mat[0].length - 1, r = new Array((m + 1) * (n + 1))
for (let i = 0, pos = 0; i <= m + n + 1; i++) {
if (i % 2 === 0) {
let x = i < m ? i : m, y = i < m ? 0 : i - m
while (x >= 0 && y <= n) {
r[pos++] = mat[x][y]
x--
y++
}
} else {
let x = i < n ? 0 : i - n, y = i < n ? i : n
while (y >= 0 && x <= m) {
r[pos++] = mat[x][y]
x++
y--
}
}
}
return r
};
class Solution {
public int[] findDiagonalOrder(int[][] mat) {
int m = mat.length - 1, n = mat[0].length - 1;
int[] r = new int[(m + 1) * (n + 1)];
for (int i = 0, pos = 0; i <= m + n + 1; i++) {
if (i % 2 == 0) {
int x = i < m ? i : m, y = i < m ? 0 : i - m;
while (x >= 0 && y <= n) {
r[pos++] = mat[x][y];
x--;
y++;
}
} else {
int x = i < n ? 0 : i - n, y = i < n ? i : n;
while (y >= 0 && x <= m) {
r[pos++] = mat[x][y];
x++;
y--;
}
}
}
return r;
}
}
class Solution:
def findDiagonalOrder(self, mat: List[List[int]]) -> List[int]:
m, n, pos = len(mat) - 1, len(mat[0]) - 1, 0
r = [0] * ((m + 1) * (n + 1))
for i in range(0, m + n + 2):
if i % 2 == 0:
x, y = i if i < m else m, 0 if i < m else i - m
while x >= 0 and y <= n:
r[pos] = mat[x][y]
pos += 1
x -= 1
y += 1
else:
x, y = 0 if i < n else i - n, i if i < n else n
while y >= 0 and x <= m:
r[pos] = mat[x][y]
pos += 1
x += 1
y -= 1
return r
func findDiagonalOrder(mat [][]int) []int {
m, n := len(mat) - 1, len(mat[0]) - 1
r := make([]int, (m + 1) * (n + 1))
for i, pos := 0, 0; i <= m + n + 1; i++ {
var x, y int
if i % 2 == 0 {
if i < m {
x = i
y = 0
} else {
x = m
y = i - m
}
for x >= 0 && y <= n {
r[pos] = mat[x][y]
pos++
x--
y++
}
} else {
if i < n {
x = 0
y = i
} else {
x = i - n
y = n
}
for y >= 0 && x <= m {
r[pos] = mat[x][y]
pos++
x++
y--
}
}
}
return r
}
class Solution {
function findDiagonalOrder($mat) {
$m = count($mat) - 1;
$n = count($mat[0]) - 1;
$r = array_fill(0, ($m + 1) * ($n + 1), 0);
for ($i = $pos = 0; $i <= $m + $n + 1; $i++) {
if ($i % 2 === 0) {
$x = $i < $m ? $i : $m;
$y = $i < $m ? 0 : $i - $m;
while ($x >= 0 && $y <= $n) {
$r[$pos++] = $mat[$x][$y];
$x--;
$y++;
}
} else {
$x = $i < $n ? 0 : $i - $n;
$y = $i < $n ? $i : $n;
while ($y >= 0 && $x <= $m) {
$r[$pos++] = $mat[$x][$y];
$x++;
$y--;
}
}
}
return $r;
}
}