对角线遍历

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
public int[] findDiagonalOrder(int[][] mat) {

if(mat.length==0||mat==null){
return null;
}else{

int top = 0;
int bottom = mat.length;
int left = 0;
int right = mat[0].length;
int row =0;
int col =0;
int index = 0;
int[] ord = new int[right*bottom];
while(row < bottom-1||col < right-1){
while(row > top && col < right-1){
ord[index] = mat[row][col];
index++;
row--;
col++;
}
ord[index] = mat[row][col];
index++;
if(col+1 < right){
col++;
while(row < bottom-1 && col > left){
ord[index] = mat[row][col];
index++;
row++;
col--;
}
ord[index] = mat[row][col];
index++;
if(row+1<bottom){
row++;
}else{
col++;
}
}else if(row+1 < bottom){
row++;
while(row < bottom-1 && col > left){
ord[index] = mat[row][col];
index++;
row++;
col--;
}
ord[index] = mat[row][col];
index++;
if(row+1<bottom){
row++;
}else{
col++;
}
}

}
if(row == bottom-1&&col==right-1){
ord[index] = mat[row][col];
}
return ord;
}
}