初始化两个坐标点,一个每次向右,到达尽头往下;一个每次向下,到达尽头往右;打印两点之间的对角线
public static void zigzag(int[][] arr){
if(arr==null){
return;
}
int ar = 0,ac = 0,br = 0,bc = 0;
int rl = arr.length-1;
int cl = arr[0].length-1;
boolean flag = false;
while(ar!=rl+1){
zPrint(arr,ar,ac,br,bc,flag);
ar = ac == cl ? ++ar:ar;
ac = ac == cl ? ac:++ac;
bc = br == rl ? ++bc:bc;
br = br == rl ? br:++br;
flag = !flag;
}
}
public static void zPrint(int[][] arr,int ar,int ac,int br,int bc,boolean flag){
if(flag){
while(ar != br+1){
System.out.print(arr[ar++][ac--]+",");
}
}else{
while(br!=ar-1){
System.out.print(arr[br--][bc++]+",");
}
}
}