解题思路
模拟题,需要模拟整个流程。
相关代码
class Solution {
public int[] findDiagonalOrder(int[][] mat) {
int d=0;
int x=0;
int y=0;
int m = mat.length;
int n = mat[0].length;
int res[] = new int[m*n];
boolean direct = true; //默认向上
while(d<m*n){
if(direct==true){
while(x>=0&&y<n){
res[d++] = mat[x][y];
x--;
y++;
}
if(y<n){
x++;
}
else{
x++;
y--;
x++;
}
}
else{
while(x<m&&y>=0){
res[d++] = mat[x][y];
x++;
y--;
}
if(x<m){
y++;
}
else{
x--;
y++;
y++;
}
}
direct = !direct;
}
return res;
}
}