问题描述
思路
同剑指offer-顺时针打印矩阵只是函数名称不一样
https://blog.csdn.net/weixin_44135282/article/details/89683461
java实现
import java.util.ArrayList;
class Solution {
public ArrayList<Integer> spiralOrder(int [][] matrix) {
ArrayList<Integer> result = new ArrayList<Integer>();
int row = matrix.length;//行数
if(row ==0)
return result;
int col = matrix[0].length;//列数
if(col==0)
return result;
int left=0;//左边界
int right=col-1;//右边界
int top=0;//上边界
int bottom=row-1;//下边界
while(top <= bottom && left <=right){
for(int i=left ; i<=right ; i++)//process1
result.add(matrix[top][i]);
if(top+1<=bottom )//process2继续的条件
for(int i=top+1 ; i<=bottom ; i++)
result.add(matrix[i][right]);//process2
if(top+1<=bottom && right-1>=left)//process3继续的条件
for(int i=right-1 ; i>=left ; i--)
result.add(matrix[bottom][i]);//process3
if(top+1<=bottom-1 && right-1>=left)//process4继续的条件
for(int i=bottom-1 ; i>=top+1 ; i--)
result.add(matrix[i][left]);//process4
top++;//上边界下移1
bottom--;//下边界上移1
left++;//左边界右移1
right--;//右边界左移1
}
return result;
}
}