public int[] spiralOrder(int[][] matrix) {
if(matrix.length==0) return new int[0];
int left=0;
int right=matrix[0].length-1;
int top=0;
int down=matrix.length-1;
int[] res=new int[(right+1)*(down+1)];
int index=0;
while(true){
for(int i=left;i<=right;i++){
res[index++]=matrix[top][i];
}
if(++top>down) break;
for(int i=top;i<=down;i++){
res[index++]=matrix[i][right];
}
if(--right<left) break;
for(int i=right;i>=left;i--){
res[index++]=matrix[down][i];
}
if(--down<top) break;
for(int i=down;i>=top;i--){
res[index++]=matrix[i][left];
}
if(++left>right) break;
}
return res;
}
08-08